---
title: "Online Supplement to Once They Are Seated: The Impact of Radical Right Parties’ Political Representation on Attitudes of Trust and Solidarity "
author: "Mikkel Haderup Larsen"
date: "`r Sys.Date()`"
output:
  html_document:
    df_print: paged
    toc: yes
    toc_depth: '2'
  pdf_document:
    toc: yes
    toc_depth: 2
  word_document:
    toc: yes
    toc_depth: '2'
geometry: margin=2cm
fontsize: 11pt
---

\newpage
# Introduction
This is the online R code supplement to "Once They Are Seated: The Impact of  Radical Right Parties’ Political Representation on Attitudes of Trust and Solidarity". If you are interested in skimming the R code that generates the results of the paper or If you wish to replicate the results themselves and maybe alter the R code, you will need to work with this Rmd file. To use the Rmd replication file, you need a working version of R (see detailed session info under "Setup" below), RStudio, RMArkdown and Latex. To install RMarkdown and Latex, use the following code after you have installed R and RStudio:

```{r eval = FALSE}
install.packages(c('rmarkdown', 'tinytex'), dep = TRUE)
tinytex::install_tinytex()  # install TinyTeX
```

```{r knitr, include = FALSE}
# Knitr options for this document
options(
  tinytex.verbose = TRUE, 
  digits = 3)

knitr::opts_chunk$set(
  warning = FALSE, message = FALSE, cache = FALSE,
  collapse = TRUE, fig.retina = 3, 
  fig.align='center', error = TRUE, digits = 2)
```

\newpage
This online supplement contains the following appendices.

*Appendix C*: Two-way FE: Replication Code of two-way fixed effects models

*Appendix D*: Two-way FE: Proportional Systems and Electoral Thresholds

*Appendix E*: Two-way FE: Influential Case analysis

*Appendix F*: Two-way FE: Pre- and post event-dummies

*Appendix G*: RDD: Replication code of regression discontinuity design

*Appendix H*: RDD: Continuity, within-group jumps, and bandwidth selection.

*Appendix I*: Exploratory analysis: The integration policy context as a moderator?

*Appendix J*: Replication: International Social Survey Programme data on role of government - Support for redistribution

*Appendix K*: Replication: integrated values study data - Institutional and social trust

*Appendix L*: Assessing the null-findings using Bayes factors


In order to perfectly reproduce the results, you need to ensure that you use the same versions of R and of the R-packages used by the author. The session info at the bottom of this code chunk provides all necessary information.

```{r setup, results='hide'}
# Load libraries
  library(tidyverse) #Tidy data
  library(haven) #Import .dta file
  library(car) # Calculate dfbetas
  library(psych) # Multiple-item scales
  library(lubridate) # Tidy dates
  library(influence.ME) #Systematic macro outlier analysis
  library(agrmt) # Van der Eijk's agreement measure
  library(ggplot2) #Nice plots
  library(directlabels) # Label plots
  library(ggpubr) #Arrange plots
  library(ggExtra) #Save plots
  library(ggeffects) # Interaction plots
  library(estimatr) #Robust linear models
  library(lme4) # Multi-level modelling
  library(rdrobust) # Regression discontinuity design
  library(BayesFactor)
  library(gtsummary) # Descriptive statistics
  library(gt) # Publication-ready tables
  library(texreg) # texreg object
  library(devtools)
  library(feisr)

# Show session info on used packages and versions
sessionInfo()
```

\newpage
# Appendix C: Replication code - Two-way fixed effects models

```{r FE prep, results='asis'}
# Load data #
setwd("")
FE <- read_dta("ESS DATA") %>% select(cntry, inwyr, inwyys, essround, inwdd, inwdds, inwmm, inwmms, inwyr, inwyys, imbgeco, imueclt, imwbcnt, imsmetn, imdfetn, impcntr, imsclbn, gincdif, trstprl, trstplt, trstlgl, ppltrst, pplfair, pplhlp, edulvla, pspwght, gndr, agea, lrscale, mnactic, rlgatnd, rlgdgr, hhmmb, brncntr, blgetmg, vote, wteven)

FE <- FE %>%
  mutate(Country = case_when(
                          cntry== "CY" ~ "Cyprus",
                          cntry=="CZ" ~ "Czechia",
                          cntry=="DE" ~ "Germany",
                          cntry=="EE" ~ "Estonia",
                          cntry=="ES" ~ "Spain",
                          cntry=="FR" ~ "France",
                          cntry=="GB" ~ "UK",
                          cntry=="GR" ~ "Greece",
                          cntry=="HU" ~ "Hungary",
                          cntry=="IE" ~ "Ireland",
                          cntry=="IS" ~ "Iceland",
                          cntry=="LT" ~ "Lithuania",
                          cntry=="LU" ~ "Luxembourg",
                          cntry=="PT" ~ "Portugal",
                          cntry=="SE" ~ "Sweden",
                          cntry=="UA" ~ "Ukraine",
                          cntry== "SK" ~ "Slovakia") %>% factor(),
         Time = case_when(
           inwyr == 2002 ~ 2002,
           inwyr == 2003 ~ 2003,
           inwyr == 2004 ~ 2004,
           inwyr == 2005 ~ 2005,
           inwyr == 2006 ~ 2006,
           inwyys == 2006 ~ 2006,
           inwyys == 2007 ~ 2007,
           inwyys == 2008 ~ 2008,
           inwyys == 2009 ~ 2009,
           inwyys == 2010 ~ 2010,
           inwyys == 2011 ~ 2011,
           inwyys == 2012 ~ 2012,
           inwyys == 2013 ~ 2013,
           inwyys == 2014 ~ 2014,
           inwyys == 2015 ~ 2015,
           inwyys == 2016 ~ 2016,
           inwyys == 2017 ~ 2017,
           inwyys == 2018 ~ 2018,
           inwyys == 2019 ~ 2019,
           inwyys == 2020 ~ 2020),
countryyear = case_when( # Create shared key
Country==	"Cyprus"	& 	essround==	3	~	1,
Country==	"Cyprus"	& 	essround==	4	~	2,
Country==	"Cyprus"	& 	essround==	5	~	3,
Country==	"Cyprus"	& 	essround==	6	~	4,
Country==	"Cyprus"	& 	essround==	9	~	5,
Country==	"Czechia"	& 	essround==	1	~	6,
Country==	"Czechia"	& 	essround==	2	~	7,
Country==	"Czechia"	& 	essround==	4	~	8,
Country==	"Czechia"	& 	essround==	5	~	9,
Country==	"Czechia"	& 	essround==	6	~	10,
Country==	"Czechia"	& 	essround==	7	~	11,
Country==	"Czechia"	& 	essround==	8	~	12,
Country==	"Czechia"	& 	essround==	9	~	13,
Country==	"Estonia"	& 	essround==	2	~	14,
Country==	"Estonia"	& 	essround==	3	~	15,
Country==	"Estonia"	& 	essround==	4	~	16,
Country==	"Estonia"	& 	essround==	6	~	17,
Country==	"Estonia"	& 	essround==	7	~	18,
Country==	"Estonia"	& 	essround==	8	~	19,
Country==	"Estonia"	& 	essround==	9	~	20,
Country==	"France"	& 	essround==	1	~	21,
Country==	"France"	& 	essround==	2	~	22,
Country==	"France"	& 	essround==	3	~	23,
Country==	"France"	& 	essround==	4	~	24,
Country==	"France"	& 	essround==	5	~	25,
Country==	"France"	& 	essround==	6	~	26,
Country==	"France"	& 	essround==	7	~	27,
Country==	"France"	& 	essround==	8	~	28,
Country==	"France"	& 	essround==	9	~	29,
Country==	"Germany"	& 	essround==	1	~	30,
Country==	"Germany"	& 	essround==	2	~	31,
Country==	"Germany"	& 	essround==	3	~	32,
Country==	"Germany"	& 	essround==	4	~	33,
Country==	"Germany"	& 	essround==	5	~	34,
Country==	"Germany"	& 	essround==	6	~	35,
Country==	"Germany"	& 	essround==	7	~	36,
Country==	"Germany"	& 	essround==	8	~	37,
Country==	"Germany"	& 	essround==	9	~	38,
Country==	"Greece"	& 	essround==	1	~	39,
Country==	"Greece"	& 	essround==	2	~	40,
Country==	"Greece"	& 	essround==	4	~	41,
Country==	"Greece"	& 	essround==	5	~	42,
Country==	"Hungary"	& 	essround==	1	~	43,
Country==	"Hungary"	& 	essround==	2	~	44,
Country==	"Hungary"	& 	essround==	3	~	45,
Country==	"Hungary"	& 	essround==	4	~	46,
Country==	"Hungary"	& 	essround==	7	~	47,
Country==	"Hungary"	& 	essround==	8	~	48,
Country==	"Hungary"	& 	essround==	5	~	49,
Country==	"Hungary"	& 	essround==	6	~	50,
Country==	"Hungary"	& 	essround==	9	~	51,
Country==	"Iceland"	& 	essround==	2	~	52,
Country==	"Iceland"	& 	essround==	6	~	53,
Country==	"Iceland"	& 	essround==	8	~	54,
Country==	"Iceland"	& 	essround==	9	~	55,
Country==	"Ireland"	& 	essround==	1	~	56,
Country==	"Ireland"	& 	essround==	2	~	57,
Country==	"Ireland"	& 	essround==	3	~	58,
Country==	"Ireland"	& 	essround==	4	~	59,
Country==	"Ireland"	& 	essround==	5	~	60,
Country==	"Ireland"	& 	essround==	6	~	61,
Country==	"Ireland"	& 	essround==	7	~	62,
Country==	"Ireland"	& 	essround==	8	~	63,
Country==	"Ireland"	& 	essround==	9	~	64,
Country==	"Lithuania"	& 	essround==	5	~	65,
Country==	"Lithuania"	& 	essround==	6	~	66,
Country==	"Lithuania"	& 	essround==	7	~	67,
Country==	"Lithuania"	& 	essround==	8	~	68,
Country==	"Lithuania"	& 	essround==	9	~	69,
Country==	"Luxembourg"	& 	essround==	1	~	70,
Country==	"Luxembourg"	& 	essround==	2	~	71,
Country==	"Portugal"	& 	essround==	1	~	72,
Country==	"Portugal"	& 	essround==	3	~	73,
Country==	"Portugal"	& 	essround==	4	~	74,
Country==	"Portugal"	& 	essround==	5	~	75,
Country==	"Portugal"	& 	essround==	6	~	76,
Country==	"Portugal"	& 	essround==	7	~	77,
Country==	"Portugal"	& 	essround==	8	~	78,
Country==	"Portugal"	& 	essround==	9	~	79,
Country==	"Portugal"	& 	essround==	2	~	80,
Country==	"Slovakia"	& 	essround==	2	~	81,
Country==	"Slovakia"	& 	essround==	6	~	82,
Country==	"Slovakia"	& 	essround==	3	~	83,
Country==	"Slovakia"	& 	essround==	4	~	84,
Country==	"Slovakia"	& 	essround==	5	~	85,
Country==	"Slovakia"	& 	essround==	9	~	86,
Country==	"Spain"	& 	essround==	1	~	87,
Country==	"Spain"	& 	essround==	2	~	88,
Country==	"Spain"	& 	essround==	3	~	89,
Country==	"Spain"	& 	essround==	4	~	90,
Country==	"Spain"	& 	essround==	5	~	91,
Country==	"Spain"	& 	essround==	6	~	92,
Country==	"Spain"	& 	essround==	7	~	93,
Country==	"Spain"	& 	essround==	8	~	94,
Country==	"Spain"	& 	essround==	9	~	95,
Country==	"Sweden"	& 	essround==	1	~	96,
Country==	"Sweden"	& 	essround==	2	~	97,
Country==	"Sweden"	& 	essround==	3	~	98,
Country==	"Sweden"	& 	essround==	4	~	99,
Country==	"Sweden"	& 	essround==	5	~	100,
Country==	"Sweden"	& 	essround==	6	~	101,
Country==	"Sweden"	& 	essround==	7	~	102,
Country==	"Sweden"	& 	essround==	8	~	103,
Country==	"Sweden"	& 	essround==	9	~	104,
Country==	"UK"	& 	essround==	1	~	105,
Country==	"UK"	& 	essround==	2	~	106,
Country==	"UK"	& 	essround==	3	~	107,
Country==	"UK"	& 	essround==	4	~	108,
Country==	"UK"	& 	essround==	5	~	109,
Country==	"UK"	& 	essround==	6	~	110,
Country==	"UK"	& 	essround==	7	~	111,
Country==	"UK"	& 	essround==	8	~	112,
Country== "UK"  &   essround==  9 ~ 113,
Country==	"Ukraine"	& 	essround==	2	~	114,
Country==	"Ukraine"	& 	essround==	3	~	115,
Country==	"Ukraine"	& 	essround==	4	~	116,
Country==	"Ukraine"	& 	essround==	5	~	117,
Country==	"Ukraine"	& 	essround==	6	~	118))

FE <- read_dta("ESS macrodata") %>%
  select(votepct, seats, election_year, election_month, election_day, parliament, treated, term, gov, history, density, perc_foreign, gdp, unemployment, gini, countryyear, entrance_year, entrance_month, entrance_day, mean_unemployment, mean_gini, mean_foreign, mean_gdp, mean_density) %>% left_join(x = FE, y= ., by ="countryyear")

# recode treatment + covariates #
FE <- FE %>%
mutate(
  ID = row_number(),
  Born_country = case_when(
    brncntr == 1 ~ "Yes",
    brncntr == 2 ~ "No") %>% factor() %>%
    fct_relevel("Yes", "No"),
  Majority = case_when(
    blgetmg == 2 ~ "Yes",
    blgetmg == 1 ~ "No") %>% factor() %>%
    fct_relevel("Yes", "No"),
  Eligible_vote = case_when(
    vote < 3 ~ "Yes",
    vote == 3 ~ "No") %>% factor() %>%
    fct_relevel("Yes", "No"),
  Parliament = case_when(
    parliament == 1 ~ "RRP in parliament",
    parliament == 0 ~ "RRP not in parliament") %>% factor() %>%
    fct_relevel("RRP not in parliament", "RRP in parliament"),
  Treated = case_when(
    treated == 1 ~ "Treated countries",
    treated == 0 ~ "Comparison countries") %>% factor() %>%
    fct_relevel("Comparison countries", "Treated countries"),
  Term_dummies = case_when(
      term == 0 ~ "Untreated",
      term == 1 ~ "First entrance",
      term == 2 ~ "Second entrance",
      term == 3 | term == 4 ~ "Third entrance") %>% factor() %>%
        fct_relevel("Untreated",
                    "First entrance",
                    "Second entrance",
                    "Third entrance"),
  History = case_when(
    history == -5 | history == -4 |  history == -3 |history == -2 | history == 0 ~ "Control group",
    history == -1 ~ "-1",
    history == 1 ~ "1",
    history == 2  ~ "2",
    history == 3 | history == 4 | history == 5 ~ "3+") %>% factor() %>%
        fct_relevel("Control group",
                    "-1",
                    "1",
                    "2",
                    "3+"),
  Gov = case_when(
    gov == 0 ~ "Left-wing government",
    gov == 1 ~ "Right-wing government")%>% factor() %>%
        fct_relevel("Left-wing government",
                    "Right-wing government"),
  Education = case_when(
    edulvla == 1 ~ "Primary",
    edulvla == 2 ~ "Secondary",
    edulvla == 3 ~ "Upper secondary",
    edulvla == 4 ~ "Post-secondary",
    edulvla == 5 ~ "Tertiary") %>% factor() %>%
    fct_relevel("Primary",
                "Secondary",
                "Upper secondary",
                "Post-secondary",
                "Tertiary"),
  Female = case_when(
    gndr == 1 ~ "Male",
    gndr == 2 ~ "Female") %>% factor() %>%
      fct_relevel("Male", "Female"),
  Employment = case_when(
    mnactic == 1 ~ "Employed",
    mnactic == 2 ~ "Student",
    mnactic > 2 & mnactic < 5 ~ "Unemployed",
    mnactic > 4 & mnactic < 10 ~ "Outside labour force") %>% factor() %>% 
    fct_relevel("Employed",
                "Unemployed",
                "Student",
                "Outside labour force"),
  Agea = case_when(
    agea < 18 ~ NA_real_, 
                        TRUE ~ zap_labels(agea)),
  Age = (Agea - mean(Agea, na.rm=TRUE)) / sd(Agea, na.rm = TRUE),
  Religious_attendance = case_when(
    rlgatnd == 7 ~ "Never",
    rlgatnd > 4 & rlgatnd < 7 ~ "Rarely",
    rlgatnd < 5 ~ "Regularly") %>% factor %>%
      fct_relevel("Never",
                  "Rarely",
                  "Regularly"),
  Household_size = case_when(
    hhmmb > 30 ~ NA_real_, 
                        TRUE ~ zap_labels(hhmmb)),
  Household_size = (Household_size - mean(Household_size, na.rm=TRUE)) / sd(Household_size, na.rm = TRUE),
  Voteshare = votepct,
  Seatshare = seats,
  Lnunemployment = (log(unemployment) - mean(log(unemployment), na.rm=TRUE)) / sd(log(unemployment), na.rm = TRUE),
  Lngdp = (log(gdp) - mean(log(gdp), na.rm=TRUE)) / sd(log(gdp), na.rm = TRUE),
  Foreign = (perc_foreign - mean(perc_foreign, na.rm=TRUE)) / sd(perc_foreign, na.rm = TRUE),
  Density = (density - mean(density, na.rm=TRUE)) / sd(density, na.rm = TRUE),
  Gini = (gini - mean(gini, na.rm=TRUE)) / sd(gini, na.rm = TRUE),
  Mean_unemployment = mean_unemployment,
  Mean_gdp = mean_gdp,
  Mean_foreign = mean_foreign,
  Mean_gini = mean_gini,
  Mean_density = mean_density,
  Country = as_factor(Country),
  Countryyear  = as_factor(countryyear),
  Time = as_factor(Time),
  ESS_weight = pspwght,
  Weight = wteven,
  day = coalesce(inwdd, inwdds),
  month = coalesce(inwmm, inwmms),
  year = coalesce(inwyr, inwyys),
  Interview_date = make_date(year = year, month = month, day = day),
  Election_date = make_date(year=election_year, month = election_month, day = election_day),
  Entrance_date = make_date(year=entrance_year, month= entrance_month, day = entrance_day),
  dist = (Interview_date - Election_date) %>% as.numeric(),
  distance_election = case_when(
    dist < 1 ~ NA_real_,
    TRUE ~ zap_labels(dist)),
  Distance_election = (distance_election - mean(distance_election, na.rm=TRUE)) / sd(distance_election, na.rm = TRUE),
  Distance_entrance = (Interview_date - Entrance_date))

# Recode outcomes #
## Reverse scales
FE$gincdif <- recode(FE$gincdif, "1=5; 2=4; 3=3; 4=2; 5=1")
FE$imbgeco <- recode(FE$imbgeco, "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1")
FE$imueclt <- recode(FE$imueclt, "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1")
FE$imwbcnt <- recode(FE$imwbcnt, "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1")

## Mutate
FE <- FE %>%
  mutate(
    Econ_threat = case_when(
      imbgeco > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(imbgeco)),
    Cul_threat = case_when(
      imueclt > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(imueclt)),
    Soc_threat = case_when(
      imwbcnt > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(imwbcnt)),
    redistribution = case_when(
      gincdif > 5 ~ NA_real_, 
                        TRUE ~ zap_labels(gincdif)),
    Redistribution = (redistribution - mean(redistribution, na.rm=TRUE)) / sd(redistribution, na.rm = TRUE),
    Trust_parliament = case_when(
      trstprl > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(trstprl)),
    Trust_politicians = case_when(
      trstplt > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(trstplt)),
    Trust_courts = case_when(
      trstlgl > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(trstlgl)),
    Trust = case_when(
      ppltrst > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(ppltrst)),
    Fair = case_when(
      pplfair > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(pplfair)),
    Help = case_when(
      pplhlp > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(pplhlp)))

## Calculate weights
#FE$W <- sum(FE$pspwght)
#w <- group_by(FE, Countryyear) %>% summarise(w = sum(FE$pspwght))
#FE <- left_join(FE, w, by=c("Countryyear"))
#FE$Weight <- FE$pspwght * (FE$W/FE$w)

# DF containing only variables used for analysis
FE_analysis <- FE %>% select(Econ_threat, Cul_threat, Soc_threat, redistribution, Redistribution, Trust_parliament, Trust_politicians, Trust_courts, Trust, Fair, Help, Parliament, Treated, History,Term_dummies, Voteshare, Seatshare, Education, Employment, Age, Age, Female, Religious_attendance, Household_size, Distance_election, Lnunemployment, unemployment, Lngdp, gdp, Foreign, Gini, Density, Mean_unemployment, Mean_gdp, Mean_foreign, Mean_gini, Mean_density, Country, Time, Countryyear, essround, ESS_weight, Weight, Born_country, Majority, Eligible_vote, Gov) %>% drop_na()

## PCA of anti-immigrant sentiment
(pca_antiimmi <- prcomp(formula = ~ Econ_threat + Cul_threat + Soc_threat, data = FE_analysis, na.action = na.exclude, retx=TRUE))
summary(pca_antiimmi)

FE_analysis$Antiimmi <- pca_antiimmi$x[ , "PC1"] # Generate new variable containing the PC1 scores.

## Polarization measures of Anti-immigrant sentiment

### Squared distance from country-year mean
FE_analysis <- FE_analysis %>%
group_by(Countryyear) %>%
mutate(
  Antiimmi_countryyear = mean(Antiimmi, na.rm=TRUE))

FE_analysis$Pol_antiimmi = (FE_analysis$Antiimmi - FE_analysis$Antiimmi_countryyear)^2
 
FE_analysis$Antiimmi_scale = FE_analysis$Econ_threat + FE_analysis$Cul_threat + FE_analysis$Soc_threat 

### Van der Eijk's agreement measure
FE_analysis <- FE_analysis %>%
  group_by(Countryyear) %>%
  mutate(
  Antiimmi_van_der_eijk = polarization(collapse(Antiimmi_scale)))

## PCA of institutional trust
(pca_inst <- prcomp(formula = ~ Trust_parliament + Trust_politicians + Trust_courts, data = FE_analysis, na.action = na.exclude, retx=TRUE))
summary(pca_inst)

FE_analysis$Institutional_trust <- pca_inst$x[ , "PC1"] # Generate new variable containing the PC1 scores.

## PCA of generalized social trust
(pca_trust <- prcomp(formula = ~ Trust + Fair + Help, data = FE_analysis, na.action = na.exclude, retx=TRUE))
summary(pca_trust)

FE_analysis$Social_trust <- pca_trust$x[ , "PC1"] # Generate new variable containing the PC1 scores.

## Welfare chauvinism
FE <- FE %>%
  mutate(
imsclbn1 = case_when(
  imsclbn > 5 ~ NA_real_,
  TRUE ~ zap_labels(imsclbn)),
Wc = (imsclbn1 - mean(imsclbn1, na.rm=TRUE)) / sd(imsclbn1, na.rm = TRUE))
```

\newpage
## Table C1: Two-way FE regression models
```{r FE analysis - table, results='hide'}
## Anti-immigrant sentiment
mod_H1a <- lm_robust(data = FE_analysis,
                   Antiimmi ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_H1a)

mod_H1b <- lm_robust(data = FE_analysis,
                   Pol_antiimmi ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_H1b)

## Welfare attitudes

mod_H2a <- lm_robust(data = FE_analysis,
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + essround,
                   weights = Weight)
summary(mod_H2a, include.ci=TRUE)

mod_H2b <- lm_robust(data = FE,
                   Wc ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + essround,
                   weights = Weight)
summary(mod_H2b)

## Institutional trust

mod_H3a <- lm_robust(data = FE_analysis,
                   Institutional_trust ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_H3a)

## Social trust

mod_H4a <- lm_robust(data = FE_analysis,
                   Social_trust ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_H4a)

htmlreg(list(mod_H1a, mod_H1b, mod_H2a, mod_H2b, mod_H3a, mod_H4a),
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.header = list("Anti-immigrant sentiment" = 1, "Polarization of immigration attitudes" = 2, "Support for redistribution"=3, "Welfare chauvinism" =4, "Institutional trust"=5, "Social trust"=6),
        custom.coef.names = c("RRP in parliament", "RRP vote share in %", 
                              "Lower secondary", "Upper secondary", "Post-secondary", "Tertiary", "Age", "Female", "Rarely", "Regularly", "Unemployed", "Student", "Not in workforce", "Household size", "Not born in country of residence", "Ethnic minority", "Cannot vote", "Interview distance to previous election", "Log unemployment rate", "Log GDP", "% Foreign born", "Gini coefficient (disponible income)", "Population density"), 
        groups = list("Education (reference: No or primary education)" = 3:6, "Religious_attendance attendance (Reference: Never)" = 9:10, "Employment status (Reference: Employed)" =11:13, "Country-level factors"=19:23), 
        caption = "Table 1: Two-way fixed effects regression table",
        caption.above = TRUE, file = "Table_C1.doc", star.symbol = "\\*")
```


## Figure C1: Two-way FE regression models
```{r FE analysis - figure, results='hide'}
plotdata_FE <- bind_rows(
  tidy(mod_H1a),
  tidy(mod_H1b),
  tidy(mod_H2a),
  tidy(mod_H2b),
  tidy(mod_H3a),
  tidy(mod_H4a)) %>% filter(str_detect(term,"^Parliament"))

plotdata_FE <- plotdata_FE %>%
mutate(
    min90 = (estimate - qt(0.95, df) * std.error),
    min95 = (estimate - qt(0.975, df) * std.error),
    max90 = (estimate + qt(0.95, df) * std.error),
    max95 = (estimate + qt(0.975, df) * std.error),
    label_up = round(estimate, digits = 3),
    term = "",
Hypo = case_when(
  outcome == "Antiimmi" ~ "H1a: Anti-immigration",
  outcome == "Pol_antiimmi" ~ "H1b: Polarization immigration",
  outcome == "Redistribution" ~ "H2a: Support for redistribution",
  outcome == "Wc" ~ "H2b: Welfare chauvinism",
  outcome == "Institutional_trust" ~ "H3: Institutional trust",
  outcome == "Social_trust" ~ "H4: Social trust") %>% as_factor())

## Plot   
Figure_C1 <- ggplot(data = plotdata_FE, aes(y = estimate, x = term)) + 
  geom_hline(yintercept = 0, color = "#901A1E", lty = "dashed") +
  geom_linerange(aes(ymin = min95, ymax = max95),
    color = "#808080", position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = min90, ymax = max90),
    size = 1.1, position = position_dodge(width = 0.5)) +
  geom_point(fill = "white", size = 2.5, position = position_dodge(width = 0.5)) +
  labs(x = "Radical right party in parliament", y = "Conditional difference in attitude (in SD)") + 
  facet_wrap(Hypo ~ ., scales = "free")  + 
  coord_flip() +
  theme_bw() +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")


## Save Figure C1
ggsave("Figure_1.jpg", plot = last_plot(), width = 14, height = 10)
#ggsave("Figure_C1.svg", plot = last_plot(), width = 8, height = 10)

```

\newpage
# Appendix D:  Proportional systems and electoral thresholds
```{r specifications, results='hide'}

### Proportional Voting systems
mod_PR1a <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "Ukraine")), 
                      Antiimmi ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_PR1a)

mod_PR1b <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "Ukraine")), 
                      Pol_antiimmi ~ Parliament + Voteshare
                      + Education + Age + Female + Religious_attendance 
                      + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                      Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_PR1b)


mod_PR2a <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "Ukraine")),
                      Redistribution ~ Parliament + Voteshare
                      + Education + Age + Female + Religious_attendance 
                      + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                      Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_PR2a)

mod_PR3a <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "Ukraine")),
                      Institutional_trust ~ Parliament + Voteshare
                      + Education + Age + Female + Religious_attendance 
                      + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                      Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_PR3a)

mod_PR4a <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "Ukraine")),
                      Social_trust ~ Parliament + Voteshare
                      + Education + Age + Female + Religious_attendance 
                      + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                      Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_PR4a)


htmlreg(list(mod_PR1a,mod_PR1b, mod_PR2a,mod_PR3a, mod_PR4a),
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.header = list("Anti-immigration" = 1, "Polarization of immigrant attitudes" =2, "Redistribution"=3, "Institutional trust"=4, "Social trust"=5),
        custom.coef.names = c("RRP in parliament", "RRP vote share in %", 
                              "Lower secondary", "Upper secondary", "Post-secondary", "Tertiary", "Age", "Female", "Rarely", "Regularly", "Unemployed", "Student", "Not in workforce", "Household size", "Not born in country of residence", "Ethnic minority", "Cannot vote", "Interview distance to previous election", "Log unemployment rate", "Log GDP", "% Foreign born", "Gini coefficient (disponible income)", "Population density"), 
        groups = list("Education (reference: No or primary education)" = 3:6, "Religious_attendance attendance (Reference: Never)" = 9:10, "Employment status (Reference: Employed)" =11:13, "Country-level factors"=19:23), 
        caption = "Table B1a: Only proportional voting systems",
        caption.above = TRUE, file = "Table_D1a.doc", star.symbol = "\\*")


### Countries with electoral threshold

mod_thres1a <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Hungary", "Iceland", "Lithuania", "Slovakia", "Sweden", "Ukraine")),
                      Antiimmi ~ Parliament + Voteshare
                      + Education + Age + Female + Religious_attendance 
                      + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                      Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_thres1a)

mod_thres1b <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Hungary", "Iceland", "Lithuania", "Slovakia", "Sweden", "Ukraine")),
                      Pol_antiimmi ~ Parliament + Voteshare
                      + Education + Age + Female + Religious_attendance 
                      + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                      Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_thres1b)



mod_thres2a <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Hungary", "Iceland", "Lithuania", "Slovakia", "Sweden", "Ukraine")),
                      Redistribution ~ Parliament + Voteshare
                      + Education + Age + Female + Religious_attendance 
                      + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                      Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_thres2a)

mod_thres3a <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Hungary", "Iceland", "Lithuania", "Slovakia", "Sweden", "Ukraine")),
                      Institutional_trust ~ Parliament + Voteshare
                      + Education + Age + Female + Religious_attendance 
                      + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                      Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_thres3a)

mod_thres4a <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Greece", "Hungary", "Iceland", "Lithuania", "Slovakia", "Sweden", "Ukraine")),
                      Social_trust ~ Parliament + Voteshare
                      + Education + Age + Female + Religious_attendance 
                      + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                      Lnunemployment + Lngdp + Foreign + Gini + Density,
                      clusters = Country,
                      se_type = "stata",
                      fixed_effects = ~ Country + Time,
                      weights = Weight)
summary(mod_thres4a)

htmlreg(list(mod_thres1a, mod_thres1b, mod_thres2a, mod_thres3a, mod_thres4a),
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.header = list("Anti-immigration" = 1, "Polarization of immigrant attitudes" =2, "Redistribution"=3, "Institutional trust"=4, "Social trust"=5),
        custom.coef.names = c("RRP in parliament", "RRP vote share in %", 
                              "Lower secondary", "Upper secondary", "Post-secondary", "Tertiary", "Age", "Female", "Rarely", "Regularly", "Unemployed", "Student", "Not in workforce", "Household size", "Not born in country of residence", "Ethnic minority", "Cannot vote", "Interview distance to previous election", "Log unemployment rate", "Log GDP", "% Foreign born", "Gini coefficient (disponible income)", "Population density"), 
        groups = list("Education (reference: No or primary education)" = 3:6, "Religious_attendance attendance (Reference: Never)" = 9:10, "Employment status (Reference: Employed)" =11:13, "Country-level factors"=19:23), 
        caption = "Table B1b: Only countries with a legal electoral threshold",
        caption.above = TRUE, file = "Table_D1b.doc", star.symbol = "\\*")

plotdata <- bind_rows(
  tidy(mod_H1a),
  tidy(mod_H1b),
  tidy(mod_H2a),
  tidy(mod_H3a),
  tidy(mod_H4a),
  tidy(mod_PR1a),
  tidy(mod_PR1b),
  tidy(mod_PR2a),
  tidy(mod_PR3a),
  tidy(mod_PR4a),
  tidy(mod_thres1a),
  tidy(mod_thres1b),
  tidy(mod_thres2a),
  tidy(mod_thres3a),
  tidy(mod_thres4a)
  ) %>% filter(str_detect(term,"^Parliament"))

plotdata <- plotdata %>%
mutate(
    min90 = (estimate - qt(0.95, df) * std.error),
    min95 = (estimate - qt(0.975, df) * std.error),
    max90 = (estimate + qt(0.95, df) * std.error),
    max95 = (estimate + qt(0.975, df) * std.error),
    label_up = round(estimate, digits = 3),
    Model = case_when(
    df == 16 ~ "Full sample",
    df == 13 ~ "Proportional systems",
    df == 10 ~ "Legal electoral threshold") %>% as_factor(),
    term = "",
Hypo = case_when(
  outcome == "Antiimmi" ~ "H1a: Anti-immigration",
  outcome == "Pol_antiimmi" ~ "H1b: Pol. immigration",
  outcome == "Redistribution" ~ "H2a: Redistribution",
  outcome == "Institutional_trust" ~ "H3: Inst. trust",
  outcome == "Social_trust" ~ "H4: Soc.trust") %>% as_factor())

Figure_D1 <- ggplot(data = plotdata, aes(y = estimate, x = term, shape = Model)) + 
  geom_hline(yintercept = 0, color = "#901A1E", lty = "dashed") +
  geom_linerange(aes(ymin = min95, ymax = max95),
    color = "#808080", position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = min90, ymax = max90),
    size = 1.1, position = position_dodge(width = 0.5)) +
  geom_point(fill = "white", size = 2.5, position = position_dodge(width = 0.5)) +
  scale_shape_manual(values = c(15, 16, 17)) +
  scale_y_continuous(breaks = seq(from = -2, to = 4, by = 0.5)) +
  labs(x = "Populist Radical Right Party in Parliament", y = "Conditional difference (in SD)") + 
  facet_wrap(Hypo ~ ., scales = "free")  + 
  coord_flip() +
  theme_bw() +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")

#### Save Figure D1
ggsave("Figure_D1.pdf", plot = last_plot(), width = 8, height = 10)
#ggsave("Figure_D1.svg", plot = last_plot(), width = 8, height = 10)
```

\newpage
# Appendix E
## DFbetas

```{r Outlier, results='hide'}
FE_analysis <- FE_analysis %>%
  mutate(
    Mean_lnunemployment = log(Mean_unemployment),
    Mean_lngdp = log(Mean_gdp),
    Long_lnunemployment = Lnunemployment -Mean_lnunemployment,
    Long_lngdp = Lngdp - Mean_lngdp,
    Long_foreign = Foreign - Mean_foreign,
    Long_gini = Gini - Mean_gini,
    Long_density = Density - Mean_density)

## Step 1: Run regression model.
Multimod_H2 <- lmer(Redistribution ~ Parliament + Voteshare +
                    + Education + Age + Female + Religious_attendance 
                    + Employment + Household_size + Born_country + Majority + Distance_election + Time 
                    + Mean_lnunemployment + Long_lnunemployment + Mean_lngdp + Long_lngdp + Mean_foreign + Long_foreign + Mean_gini + Long_gini + Mean_density + Long_density
                    + (1 | Country) + (1 | Countryyear), weights = ESS_weight, data = FE_analysis %>% filter(Eligible_vote == "Yes"), REML = TRUE)
screenreg(Multimod_H2)

# NOTE! I refrain from running the following lines of code in the analysis as the influence function has a long run time. Remove # to run the code. 

## Step 2: Calculate dfbetas
'one_out_model <- influence(model = Multimod_H2, group = "Countryyear")

(DFBETAs <- dfbetas(one_out_model) %>%
               data.frame() %>%
                rownames_to_column() %>%
                as_tibble() %>%
                select(rowname, ParliamentRRP.in.parliament))

## Step 3: Plot the dfbetas
DFBETAs <- pivot_longer(
        DFBETAs,
        cols = "ParliamentRRP.in.parliament",
        names_to = "Variable")

DFbetaplot <- ggplot(data = DFBETAs,
       aes(y = value, x = Variable, label = rowname)) +
        geom_violin(color = "gray") +
        geom_text(position = position_jitter(width=0.2,height=0)) +
        geom_hline(yintercept = 2/sqrt(112), color = "#901A1E") +
        geom_hline(yintercept = -2/sqrt(112), color = "#901A1E") +
        theme_minimal() +
        labs(y = "DFBETA")

ggsave("Figure_E1.pdf", plot = last_plot(), width = 8, height = 6)
#ggsave("Figure_E1.svg", plot = last_plot(), width = 8, height = 6)'

## Step 4: Re-estimate regression model without influential cases

mod_H2_noout <- lm_robust(data = FE_analysis %>% filter(!Countryyear %in% c("37", "53", "69", "70", "81", "84", "85", "94", "187", "208", "212", "217")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Gini + Foreign + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_H2_noout)

htmlreg(list(mod_H2a, mod_H2_noout),
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.header = list("With influential cases" = 1, "Without influential cases" = 2),
        custom.coef.names = c("RRP in parliament", "RRP vote share in %", 
                              "Lower secondary", "Upper secondary", "Post-secondary", "Tertiary", "Age", "Female", "Rarely", "Regularly", "Unemployed", "Student", "Not in workforce", "Household size", "Not born in country of residence", "Ethnic minority", "Cannot vote", "Interview distance to previous election", "Log unemployment rate", "Log GDP", "% Foreign born", "Gini coefficient (disponible income)", "Population density"), 
        groups = list("Education (reference: No or primary education)" = 3:6, "Religious_attendance attendance (Reference: Never)" = 9:10, "Employment status (Reference: Employed)" =11:13, "Country-level factors"=19:23), 
        caption = "Table E: Regression table - Without influential cases",
        caption.above = TRUE, file = "Table_E1.doc", star.symbol = "\\*")
```

## Leave-one-out-models
```{r Leave-one-out, results='hide'}
NoCyprus <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Czechia", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoCyprus)

NoCzech <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal","Slovakia", "Spain", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoCzech)

NoEstonia <- lm_robust(data = FE_analysis  %>% filter(Country %in% c("Cyprus", "Czechia", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoEstonia)

Nofrance <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "Germany", "Hungary", "Greece", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Nofrance)

NoGermany<- lm_robust(data = FE_analysis  %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "France", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoGermany)

NoGreece <- lm_robust(data = FE_analysis  %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "France", "Germany", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoGreece)

NoHungary <- lm_robust(data = FE_analysis  %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "France", "Germany", "Greece", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoHungary)

NoPortugal <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Slovakia", "Spain", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size  + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoPortugal)


NoSlovakia <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Spain", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size  + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoSlovakia)

NoSpain <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Sweden", "UK", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoSpain)


NoSweden <- lm_robust(data = FE_analysis %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "UK", "Ukraine")),
                   Redistribution ~ Parliament
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote +  Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoSweden)


NoUK <- lm_robust(data = FE_analysis  %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "Ukraine")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoUK)


NoUkraine <- lm_robust(data = FE_analysis  %>% filter(Country %in% c("Cyprus", "Czechia", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Lithuania", "Luxembourg", "Portugal", "Slovakia", "Spain", "Sweden", "UK")),
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(NoUkraine)

data_leave_one_out <- bind_rows(
  tidy(NoCyprus),
  tidy(NoCzech),
  tidy(NoEstonia),
  tidy(Nofrance),
  tidy(NoGermany),
  tidy(NoGreece),
  tidy(NoHungary),
  tidy(NoPortugal),
  tidy(NoSlovakia),
  tidy(NoSpain),
  tidy(NoSweden),
  tidy(NoUK),
  tidy(NoUkraine)) %>% filter(str_detect(term,"^Parliament"))

data_leave_one_out <- data_leave_one_out %>%
mutate(
    min90 = (estimate - qt(0.95, df) * std.error),
    min95 = (estimate - qt(0.975, df) * std.error),
    max90 = (estimate + qt(0.95, df) * std.error),
    max95 = (estimate + qt(0.975, df) * std.error),
    label_up = round(estimate, digits = 3))
data_leave_one_out$Country <- c("No Cyprus", " No Czech Republic", 
                                "No Estonia", "No France", "No Germany", "No Greece", "No Hungary", "No Portugal", "No Slovakia", "No Spain", "No Sweden", "No UK", "No Ukraine") %>% as_factor() %>% fct_relevel("No Cyprus", " No Czech Republic", "No Estonia", "No France", "No Germany", "No Greece", "No Hungary", "No Portugal", "No Slovakia", "No Spain", "No Sweden", "No UK", "No Ukraine")

## Plot   
Figure_E2 <- ggplot(data = data_leave_one_out, aes(y = estimate, x = Country)) + 
  geom_hline(yintercept = 0, color = "#901A1E", lty = "dashed") +
  geom_linerange(aes(ymin = min95, ymax = max95),
    color = "#808080", position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = min90, ymax = max90),
    size = 1.1, position = position_dodge(width = 0.5)) +
  geom_point(fill = "white", size = 2.5, position = position_dodge(width = 0.5)) +
  geom_text(aes(label = label_up), position = position_dodge(width = 0.5), vjust = -0.5, hjust = 0.5) +
  labs(x = "Populist Radical Right Party in Parliament", y = "Conditional difference (in SD) in support for redistribution") + 
  coord_flip() +
  theme_bw() +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")

## Save Figure E2
ggsave("Figure_E2.pdf", plot = last_plot(), width = 8, height = 6)
#ggsave("Figure_C2.svg", plot = last_plot(), width = 8, height = 6)
```

\newpage
# Appendix F: Pre-trends and effect duration
```{r prepost, results='hide'}
## Step 1: Run regression
mod_H2_dummies <- lm_robust(data = FE_analysis,
                   Redistribution ~ Term_dummies + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_H2_dummies)

## Step 2: Plot

plotdata_entrances <- bind_rows(
  tidy(mod_H2_dummies)) %>% filter(str_detect(term,"^Term"))

plotdata_entrances <- plotdata_entrances %>%
mutate(
    min90 = (estimate - qt(0.95, df) * std.error),
    min95 = (estimate - qt(0.975, df) * std.error),
    max90 = (estimate + qt(0.95, df) * std.error),
    max95 = (estimate + qt(0.975, df) * std.error),
    label_up = round(estimate, digits = 3),
    Term = case_when(
      term == "Term_dummiesFirst entrance" ~ "1",
      term == "Term_dummiesSecond entrance" ~ "2",
      term == "Term_dummiesThird entrance" ~ "3"))

## Plot   
Entrance <- ggplot(data = plotdata_entrances, aes(y = estimate, x = Term)) + 
  geom_hline(yintercept = 0, color = "#901A1E", lty = "dashed") +
  geom_linerange(aes(ymin = min95, ymax = max95),
    color = "#808080", position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = min90, ymax = max90),
    size = 1.1, position = position_dodge(width = 0.5)) +
  geom_point(fill = "white", size = 2.5, position = position_dodge(width = 0.5)) +
  geom_text(aes(label = label_up), position = position_dodge(width = 0.5), hjust = 1.2, vjust = -1, size = 3) +
  labs(x = "Number of RRP entries", y = "Conditional difference in support for redistribution (SD)") + 
  theme_bw() +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")
Entrance

ggsave("Figure_F2.pdf", plot = last_plot(), width = 10, height = 6)

## Step 1: Run regression
mod_H2_history <- lm_robust(data = FE_analysis,
                   Social_trust ~ History + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_H2_history)

## Step 2: Plot
plotdata_history <- bind_rows(
  tidy(mod_H2_history)) %>% filter(str_detect(term,"^History"))

plotdata_history <- plotdata_history %>%
mutate(
    min90 = (estimate - qt(0.95, df) * std.error),
    min95 = (estimate - qt(0.975, df) * std.error),
    max90 = (estimate + qt(0.95, df) * std.error),
    max95 = (estimate + qt(0.975, df) * std.error),
    label_up = round(estimate, digits = 3),
    Term = case_when(
      term == "History-1" ~ "-1",
      term ==	"History1" ~ "1",
      term ==	"History2" ~ "2",
      term ==	"History3+" ~ "3") %>% factor %>% 
      fct_relevel("-1",
                  "1",
                  "2",
                  "3")) 

History <- ggplot(data = plotdata_history, aes(y = estimate, x = Term)) + 
  geom_hline(yintercept = 0, color = "#901A1E", lty = "dashed") +
  geom_linerange(aes(ymin = min95, ymax = max95),
    color = "#808080", position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = min90, ymax = max90),
    size = 1.1, position = position_dodge(width = 0.5)) +
  geom_point(fill = "white", size = 2.5, position = position_dodge(width = 0.5)) +
  geom_text(aes(label = label_up), position = position_dodge(width = 0.5), hjust = 1.2, vjust = -1, size = 3) +
  labs(x = "Pre- and post RRP entrance", y = "Conditional difference in support for redistribution (SD)") + 
  theme_bw() +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")
History

ggsave("Figure_F1.pdf", plot = last_plot(), width = 10, height = 6)
```

\newpage
# Appendix G: Replication code - Regression Discontinuity Design
```{r RDD prep,  results='hide'}
# Load sample
setwd("")
RDD <- read_dta("ESS data") %>% select(cntry, inwyr, inwyys, essround, inwdd, inwdds, inwmm, inwmms, inwyr, inwyys, imbgeco, imueclt, imwbcnt, imsmetn, imdfetn, impcntr, imsclbn, gincdif, trstprl, trstplt, trstlgl, ppltrst, pplfair, pplhlp, edulvla, pspwght, gndr, agea, lrscale, mnactic, rlgatnd, rlgdgr, hhmmb, brncntr, blgetmg, stfdem, stfgov, vote, wteven)

# Join data
RDD <- RDD %>%
    mutate(Country = case_when(
                          cntry == "AT" ~ "Austria",
                          cntry == "BE" ~ "Belgium",
                          cntry == "BG" ~ "Bulgaria",
                          cntry== "CY" ~ "Cyprus",
                          cntry=="CZ" ~ "Czechia",
                          cntry == "DK" ~ "Denmark",
                          cntry=="EE" ~ "Estonia",
                          cntry== "FI" ~ "Finland",
                          cntry=="DE" ~ "Germany",                                 
                          cntry=="GR" ~ "Greece",
                          cntry=="HU" ~ "Hungary",
                          cntry=="IS" ~ "Iceland",
                          cntry=="IE" ~ "Ireland",
                          cntry== "IT" ~ "Italy",
                          cntry== "LV" ~ "Latvia",
                          cntry=="LT" ~ "Lithuania",
                          cntry=="LU" ~ "Luxembourg",
                          cntry == "NL" ~ "Netherlands",
                          cntry == "NO" ~ "Norway",
                          cntry== "PO" ~ "Poland",
                          cntry=="PT" ~ "Portugal",
                          cntry=="ES" ~ "Spain",
                          cntry=="SE" ~ "Sweden",
                          cntry == "SK" ~ "Slovakia",
                          cntry == "SI" ~ "Slovenia",
                          cntry == "CH" ~ "Switzerland",
                          cntry=="UA" ~ "Ukraine"),
         Time = case_when(
           inwyr == 2002 ~ 2002,
           inwyr == 2003 ~ 2003,
           inwyr == 2004 ~ 2004,
           inwyr == 2005 ~ 2005,
           inwyys == 2006 ~ 2006,
           inwyys == 2007 ~ 2007,
           inwyys == 2008 ~ 2008,
           inwyys == 2009 ~ 2009,
           inwyys == 2010 ~ 2010,
           inwyys == 2011 ~ 2011,
           inwyys == 2012 ~ 2012,
           inwyys == 2013 ~ 2013,
           inwyys == 2014 ~ 2014,
           inwyys == 2015 ~ 2015,
           inwyys == 2016 ~ 2016,
           inwyys == 2017 ~ 2017,
           inwyys == 2018 ~ 2018,
           inwyys == 2019 ~ 2019,
           inwyys == 2020 ~ 2020),
countryyear = case_when(
Country==	"Austria"	&	essround==	1	~	1,
Country==	"Austria"	&	essround==	2	~	2,
Country==	"Austria"	&	essround==	3	~	3,
Country==	"Austria"	&	essround==	7	~	6,
Country==	"Austria"	&	essround==	8	~	7,
Country==	"Austria"	&	essround==	9	~	8,
Country==	"Belgium"	&	essround==	1	~	9,
Country==	"Belgium"	&	essround==	2	~	10,
Country==	"Belgium"	&	essround==	3	~	11,
Country==	"Belgium"	&	essround==	4	~	12,
Country==	"Belgium"	&	essround==	5	~	13,
Country==	"Belgium"	&	essround==	6	~	14,
Country==	"Belgium"	&	essround==	7	~	15,
Country==	"Belgium"	&	essround==	8	~	16,
Country==	"Belgium"	&	essround==	9	~	17,
Country==	"Bulgaria"	&	essround==	3	~	18,
Country==	"Bulgaria"	&	essround==	4	~	19,
Country==	"Bulgaria"	&	essround==	5	~	20,
Country==	"Bulgaria"	&	essround==	6	~	21,
Country==	"Bulgaria"	&	essround==	9	~	22,
Country==	"Cyprus"	&	essround==	3	~	26,
Country==	"Cyprus"	&	essround==	4	~	27,
Country==	"Cyprus"	&	essround==	5	~	28,
Country==	"Cyprus"	&	essround==	6	~	29,
Country==	"Cyprus"	&	essround==	9	~	30,
Country==	"Czechia"	&	essround==	1	~	31,
Country==	"Czechia"	&	essround==	2	~	32,
Country==	"Czechia"	&	essround==	4	~	33,
Country==	"Czechia"	&	essround==	5	~	34,
Country==	"Czechia"	&	essround==	6	~	35,
Country==	"Czechia"	&	essround==	7	~	36,
Country==	"Czechia"	&	essround==	8	~	37,
Country==	"Czechia"	&	essround==	9	~	38,
Country==	"Denmark"	&	essround==	1	~	39,
Country==	"Denmark"	&	essround==	2	~	40,
Country==	"Denmark"	&	essround==	3	~	41,
Country==	"Denmark"	&	essround==	4	~	42,
Country==	"Denmark"	&	essround==	5	~	43,
Country==	"Denmark"	&	essround==	6	~	44,
Country==	"Denmark"	&	essround==	7	~	45,
Country==	"Denmark"	&	essround==	9	~	46,
Country==	"Estonia"	&	essround==	2	~	47,
Country==	"Estonia"	&	essround==	3	~	48,
Country==	"Estonia"	&	essround==	4	~	49,
Country==	"Estonia"	&	essround==	6	~	50,
Country==	"Estonia"	&	essround==	7	~	51,
Country==	"Estonia"	&	essround==	8	~	52,
Country==	"Estonia"	&	essround==	9	~	53,
Country==	"Finland"	&	essround==	2	~	55,
Country==	"Finland"	&	essround==	3	~	56,
Country==	"Finland"	&	essround==	4	~	57,
Country==	"Finland"	&	essround==	5	~	58,
Country==	"Finland"	&	essround==	6	~	59,
Country==	"Finland"	&	essround==	7	~	60,
Country==	"Finland"	&	essround==	8	~	61,
Country==	"Finland"	&	essround==	9	~	62,
Country==	"Germany"	&	essround==	1	~	63,
Country==	"Germany"	&	essround==	2	~	64,
Country==	"Germany"	&	essround==	3	~	65,
Country==	"Germany"	&	essround==	4	~	66,
Country==	"Germany"	&	essround==	5	~	67,
Country==	"Germany"	&	essround==	6	~	68,
Country==	"Germany"	&	essround==	7	~	69,
Country==	"Germany"	&	essround==	8	~	70,
Country==	"Germany"	&	essround==	9	~	71,
Country==	"Greece"	&	essround==	1	~	72,
Country==	"Greece"	&	essround==	2	~	73,
Country==	"Greece"	&	essround==	4	~	74,
Country==	"Greece"	&	essround==	5	~	75,
Country==	"Hungary"	&	essround==	1	~	76,
Country==	"Hungary"	&	essround==	2	~	77,
Country==	"Hungary"	&	essround==	3	~	78,
Country==	"Hungary"	&	essround==	4	~	79,
Country==	"Hungary"	&	essround==	5	~	80,
Country==	"Hungary"	&	essround==	6	~	81,
Country==	"Hungary"	&	essround==	7	~	82,
Country==	"Hungary"	&	essround==	8	~	83,
Country==	"Iceland"	&	essround==	2	~	85,
Country==	"Iceland"	&	essround==	6	~	86,
Country==	"Iceland"	&	essround==	8	~	87,
Country==	"Iceland"	&	essround==	9	~	88,
Country==	"Ireland"	&	essround==	1	~	89,
Country==	"Ireland"	&	essround==	2	~	90,
Country==	"Ireland"	&	essround==	3	~	91,
Country==	"Ireland"	&	essround==	4	~	92,
Country==	"Ireland"	&	essround==	5	~	93,
Country==	"Ireland"	&	essround==	6	~	94,
Country==	"Ireland"	&	essround==	7	~	95,
Country==	"Ireland"	&	essround==	8	~	96,
Country==	"Ireland"	&	essround==	9	~	97,
Country==	"Italy"	&	essround==	1	~	98,
Country==	"Italy"	&	essround==	2	~	99,
Country==	"Italy"	&	essround==	6	~	100,
Country==	"Italy"	&	essround==	8	~	101,
Country==	"Italy"	&	essround==	9	~	102,
Country==	"Latvia"	&	essround==	4	~	104,
Country==	"Latvia"	&	essround==	9	~	105,
Country==	"Lithuania"	&	essround==	5	~	106,
Country==	"Lithuania"	&	essround==	6	~	107,
Country==	"Lithuania"	&	essround==	7	~	108,
Country==	"Lithuania"	&	essround==	8	~	109,
Country==	"Lithuania"	&	essround==	9	~	110,
Country==	"Luxembourg"	&	essround==	1	~	111,
Country==	"Luxembourg"	&	essround==	2	~	112,
Country==	"Netherlands"	&	essround==	1	~	113,
Country==	"Netherlands"	&	essround==	2	~	114,
Country==	"Netherlands"	&	essround==	3	~	115,
Country==	"Netherlands"	&	essround==	4	~	116,
Country==	"Netherlands"	&	essround==	5	~	117,
Country==	"Netherlands"	&	essround==	6	~	118,
Country==	"Netherlands"	&	essround==	7	~	119,
Country==	"Netherlands"	&	essround==	8	~	120,
Country==	"Netherlands"	&	essround==	9	~	121,
Country==	"Norway"	&	essround==	1	~	122,
Country==	"Norway"	&	essround==	2	~	123,
Country==	"Norway"	&	essround==	3	~	124,
Country==	"Norway"	&	essround==	4	~	125,
Country==	"Norway"	&	essround==	5	~	126,
Country==	"Norway"	&	essround==	6	~	127,
Country==	"Norway"	&	essround==	7	~	128,
Country==	"Norway"	&	essround==	8	~	129,
Country==	"Norway"	&	essround==	9	~	130,
Country==	"Poland"	&	essround==	1	~	131,
Country==	"Poland"	&	essround==	2	~	132,
Country==	"Poland"	&	essround==	3	~	133,
Country==	"Poland"	&	essround==	4	~	134,
Country==	"Poland"	&	essround==	5	~	135,
Country==	"Poland"	&	essround==	6	~	136,
Country==	"Poland"	&	essround==	7	~	137,
Country==	"Poland"	&	essround==	8	~	138,
Country==	"Poland"	&	essround==	9	~	139,
Country==	"Portugal"	&	essround==	1	~	140,
Country==	"Portugal"	&	essround==	2	~	141,
Country==	"Portugal"	&	essround==	3	~	142,
Country==	"Portugal"	&	essround==	4	~	143,
Country==	"Portugal"	&	essround==	5	~	144,
Country==	"Portugal"	&	essround==	6	~	145,
Country==	"Portugal"	&	essround==	7	~	146,
Country==	"Portugal"	&	essround==	8	~	147,
Country==	"Portugal"	&	essround==	9	~	148,
Country==	"Slovakia"	&	essround==	2	~	151,
Country==	"Slovakia"	&	essround==	3	~	152,
Country==	"Slovakia"	&	essround==	4	~	153,
Country==	"Slovakia"	&	essround==	5	~	154,
Country==	"Slovakia"	&	essround==	6	~	155,
Country==	"Slovakia"	&	essround==	9	~	156,
Country==	"Slovenia"	&	essround==	1	~	157,
Country==	"Slovenia"	&	essround==	2	~	158,
Country==	"Slovenia"	&	essround==	3	~	159,
Country==	"Slovenia"	&	essround==	4	~	160,
Country==	"Slovenia"	&	essround==	5	~	161,
Country==	"Slovenia"	&	essround==	6	~	162,
Country==	"Slovenia"	&	essround==	7	~	163,
Country==	"Slovenia"	&	essround==	8	~	164,
Country==	"Slovenia"	&	essround==	9	~	165,
Country==	"Spain"	&	essround==	1	~	166,
Country==	"Spain"	&	essround==	2	~	167,
Country==	"Spain"	&	essround==	3	~	168,
Country==	"Spain"	&	essround==	4	~	169,
Country==	"Spain"	&	essround==	5	~	170,
Country==	"Spain"	&	essround==	6	~	171,
Country==	"Spain"	&	essround==	7	~	172,
Country==	"Spain"	&	essround==	8	~	173,
Country==	"Spain"	&	essround==	9	~	174,
Country==	"Sweden"	&	essround==	1	~	175,
Country==	"Sweden"	&	essround==	2	~	176,
Country==	"Sweden"	&	essround==	3	~	177,
Country==	"Sweden"	&	essround==	4	~	178,
Country==	"Sweden"	&	essround==	5	~	179,
Country==	"Sweden"	&	essround==	6	~	180,
Country==	"Sweden"	&	essround==	7	~	181,
Country==	"Sweden"	&	essround==	8	~	182,
Country==	"Sweden"	&	essround==	9	~	183,
Country==	"Switzerland"	&	essround==	1	~	184,
Country==	"Switzerland"	&	essround==	2	~	185,
Country==	"Switzerland"	&	essround==	3	~	186,
Country==	"Switzerland"	&	essround==	4	~	187,
Country==	"Switzerland"	&	essround==	5	~	188,
Country==	"Switzerland"	&	essround==	6	~	189,
Country==	"Switzerland"	&	essround==	7	~	190,
Country==	"Switzerland"	&	essround==	8	~	191,
Country==	"Switzerland"	&	essround==	9	~	192,
Country==	"Ukraine"	&	essround==	2	~	193,
Country==	"Ukraine"	&	essround==	3	~	194,
Country==	"Ukraine"	&	essround==	4	~	195,
Country==	"Ukraine"	&	essround==	5	~	196,
Country==	"Ukraine"	&	essround==	6	~	197))
RDD <- read_dta("ESS RDD macrodata") %>%
  select(voteshare, seats, threshold, running, year, month, day, parliament, density, foreign, gdp, unemployment, gini, essround, countryyear, legal_threshold, running_below, running_above, parliament_below, parliament_above) %>% left_join(x = RDD, y= ., by ="countryyear")

# recode treatment + covariates #
RDD <- RDD %>%
mutate(
  ID = row_number(),
  Born_country = case_when(
    brncntr == 1 ~ "Yes",
    brncntr == 2 ~ "No") %>% factor() %>%
    fct_relevel("Yes", "No"),
  Majority = case_when(
    blgetmg == 2 ~ "Yes",
    blgetmg == 1 ~ "No") %>% factor() %>%
    fct_relevel("Yes", "No"),
  Eligible_vote = case_when(
    vote < 3 ~ "Yes",
    vote == 3 ~ "No") %>% factor() %>%
    fct_relevel("Yes", "No"),
  Parliament = case_when(
    parliament == 1 ~ "RRP in parliament",
    parliament == 0 ~ "RRP not in parliament") %>% factor() %>%
    fct_relevel("RRP not in parliament", "RRP in parliament"),
  Parliament_above = case_when(
    parliament_above == 1 ~ "RRP in parliament",
    parliament_above == 0 ~ "RRP not in parliament") %>% factor() %>%
    fct_relevel("RRP not in parliament", "RRP in parliament"),
  Parliament_below = case_when(
    parliament_below == 1 ~ "RRP in parliament",
    parliament_below == 0 ~ "RRP not in parliament") %>% factor() %>%
    fct_relevel("RRP not in parliament", "RRP in parliament"),
  Running = running,
  Running2 = running*running,
  Running3 = running*running*running,
  Running4 = running*running*running*running,
  Running_below = running_below,
  Running_below2 = running_below*running_below,
  Running_above = running_above,
  Running_above2 = running_above*running_above,
  Education = case_when(
    edulvla == 1 ~ "Primary",
    edulvla == 2 ~ "Secondary",
    edulvla == 3 ~ "Upper secondary",
    edulvla == 4 ~ "Post-secondary",
    edulvla == 5 ~ "Tertiary") %>% factor() %>%
    fct_relevel("Primary",
                "Secondary",
                "Upper secondary",
                "Post-secondary",
                "Tertiary"),
  Female = case_when(
    gndr == 1 ~ "Male",
    gndr == 2 ~ "Female") %>% factor() %>%
      fct_relevel("Male", "Female"),
  Employment = case_when(
    mnactic == 1 ~ "Employed",
    mnactic == 2 ~ "Student",
    mnactic > 2 & mnactic < 5 ~ "Unemployed",
    mnactic > 4 & mnactic < 10 ~ "Outside labour force") %>% factor() %>% 
    fct_relevel("Employed",
                "Unemployed",
                "Student",
                "Outside labour force"),
  Agea = case_when(
    agea < 18 ~ NA_real_, 
                        TRUE ~ zap_labels(agea)),
  Age = (Agea- mean(Agea, na.rm=TRUE)) / sd(Agea, na.rm = TRUE),
  Religious_attendance = case_when(
    rlgatnd == 7 ~ "Never",
    rlgatnd > 4 & rlgatnd < 7 ~ "Rarely",
    rlgatnd < 5 ~ "Regularly") %>% factor %>%
      fct_relevel("Never",
                  "Rarely",
                  "Regularly"),
  Household_size = case_when(
    hhmmb > 30 ~ NA_real_, 
                        TRUE ~ zap_labels(hhmmb)),
  Household_size = (Household_size - mean(Household_size, na.rm=TRUE)) / sd(Household_size, na.rm = TRUE),
  Voteshare = voteshare,
  Seatshare = seats,
  Legal_threshold = legal_threshold,
  Lnunemployment = (log(unemployment) - mean(log(unemployment), na.rm=TRUE)) / sd(log(unemployment), na.rm = TRUE),
  Lngdp = (log(gdp) - mean(log(gdp), na.rm=TRUE)) / sd(log(gdp), na.rm = TRUE),
  Foreign = (foreign - mean(foreign, na.rm=TRUE)) / sd(foreign, na.rm = TRUE),
  Density = (density - mean(density, na.rm=TRUE)) / sd(density, na.rm = TRUE),
  Gini = (gini - mean(gini, na.rm=TRUE)) / sd(gini, na.rm = TRUE),
  Country = as_factor(Country),
  Countryyear  = as_factor(countryyear),
  Time = as_factor(Time),
  ESS_weight = pspwght,
  Weight = wteven,
  survey_day = coalesce(inwdd, inwdds),
  survey_month = coalesce(inwmm, inwmms),
  survey_year = coalesce(inwyr, inwyys),
  Survey_date = make_date(year = survey_year, month = survey_month, day = survey_day),
  Election_date = make_date(year=year, month = month, day = day),
  dist = (Survey_date - Election_date) %>% as.numeric(),
  distance_election = case_when(
    dist < 1 ~ NA_real_,
    TRUE ~ zap_labels(dist)),
  Distance_election = (distance_election - mean(distance_election, na.rm=TRUE)) / sd(distance_election, na.rm = TRUE))

# Recode outcomes #
RDD$gincdif <- recode(RDD$gincdif, "1=5; 2=4; 3=3; 4=2; 5=1")
RDD$imbgeco <- recode(RDD$imbgeco, "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1")
RDD$imueclt <- recode(RDD$imueclt, "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1")
RDD$imwbcnt <- recode(RDD$imwbcnt, "1=10; 2=9; 3=8; 4=7; 5=6; 6=5; 7=4; 8=3; 9=2; 10=1")

RDD <- RDD %>%
  mutate(
    Econ_threat = case_when(
      imbgeco > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(imbgeco)),
    Cul_threat = case_when(
      imueclt > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(imueclt)),
    Soc_threat = case_when(
      imwbcnt > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(imwbcnt)),
    redistribution = case_when(
      gincdif > 5 ~ NA_real_, 
                        TRUE ~ zap_labels(gincdif)),
    Redistribution = (redistribution - mean(redistribution, na.rm=TRUE)) / sd(redistribution, na.rm = TRUE),
    Trust_parliament = case_when(
      trstprl > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(trstprl)),
    Trust_politicians = case_when(
      trstplt > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(trstplt)),
    Trust_courts = case_when(
      trstlgl > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(trstlgl)),
    Trust = case_when(
      ppltrst > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(ppltrst)),
    Fair = case_when(
      pplfair > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(pplfair)),
    Help = case_when(
      pplhlp > 10 ~ NA_real_, 
                        TRUE ~ zap_labels(pplhlp)))

# DF retaining only variables used for analysis
RDD_analysis <- RDD %>% select(Econ_threat, Cul_threat, Soc_threat, redistribution, Redistribution, Trust_parliament, Trust_politicians, Trust_courts, Trust, Fair, Help, Parliament, Running, Running2, Running3, Running4, Voteshare, Seatshare, Education, Employment, Age, Age, Female, Religious_attendance, Household_size, Household_size, Distance_election, Lnunemployment, Lngdp, Foreign, Gini, Density, Country, Time, Countryyear, ESS_weight, pspwght, Weight, Born_country, Majority, Eligible_vote, Legal_threshold) %>% drop_na()

# Principal component analysis of outcome variables #
  
## PCA of Anti-immigrant sentiment
(pca_antiimmi <- prcomp(formula = ~ Econ_threat + Cul_threat + Soc_threat, data = RDD_analysis, na.action = na.exclude, retx=TRUE))
summary(pca_antiimmi)

RDD_analysis$Antiimmi <- pca_antiimmi$x[ , "PC1"] # Generate new variable containing the PC1 scores.

## Polarization measures of Anti-immigrant sentiment

### Squared distance from country-year mean
RDD_analysis <- RDD_analysis %>%
group_by(Countryyear) %>%
mutate(
  Antiimmi_countryyear = mean(Antiimmi, na.rm=TRUE))

RDD_analysis$Pol_antiimmi = (RDD_analysis$Antiimmi - RDD_analysis$Antiimmi_countryyear)^2
 
RDD_analysis$Antiimmi_scale = RDD_analysis$Econ_threat + RDD_analysis$Cul_threat + RDD_analysis$Soc_threat 

### Van der Eijk's agreement measure
RDD_analysis <- RDD_analysis %>%
  group_by(Countryyear) %>%
  mutate(
  Antiimmi_van_der_eijk = polarization(collapse(Antiimmi_scale)))

## PCA of Institutional trust
(pca_inst <- prcomp(formula = ~ Trust_parliament + Trust_politicians + Trust_courts, data = RDD_analysis, na.action = na.exclude, retx=TRUE))
summary(pca_inst)

RDD_analysis$Institutional_trust <- pca_inst$x[ , "PC1"] # Generate new variable containing the PC1 scores.

## PCA of Generalized social trust
(pca_trust <- prcomp(formula = ~ Trust + Fair + Help, data = RDD_analysis, na.action = na.exclude, retx=TRUE))
summary(pca_trust)

RDD_analysis$Social_trust <- pca_trust$x[ , "PC1"] # Generate new variable containing the PC1 scores.

## Welfare chauvinism
RDD <- RDD %>%
  mutate(
imsclbn1 = case_when(
  imsclbn > 5 ~ NA_real_,
  TRUE ~ zap_labels(imsclbn)),
Wc = (imsclbn1 - mean(imsclbn1, na.rm=TRUE)) / sd(imsclbn1, na.rm = TRUE))
```

## RDD estimates
```{r RDD analysis - table,  results='hide'}
# Analysis

##Second-order polynomial

mod_RDD1a <- lm_robust(data = RDD_analysis,
                   Antiimmi ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD1a)

mod_RDD1b <- lm_robust(data = RDD_analysis,
                   Pol_antiimmi ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD1b)

mod_RDD2a <- lm_robust(data = RDD_analysis,
                   Redistribution ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD2a)

mod_RDD2b <- lm_robust(data = RDD,
                   Wc ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD2b)

mod_RDD3 <- lm_robust(data = RDD_analysis,
                   Institutional_trust ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD3)

mod_RDD4 <- lm_robust(data = RDD_analysis,
                   Social_trust ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD4)

htmlreg(list(mod_RDD1a, mod_RDD1b, mod_RDD2a, mod_RDD2b,mod_RDD3, mod_RDD4),
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.header = list("Anti-immigration" = 1, "Polarization" = 2, "Support for redistribution"=3, "Welfare chauvinism"=4, "Institutional trust"=5, "Social trust"=6),
        custom.coef.names = c("RRP in parliament", "RRP vote share in % relative to threshold", "Squared RRP vote share in % relative to threshold", "RRP vote share",
                              "Lower secondary", "Upper secondary", "Post-secondary", "Tertiary", "Age in years (Z-score)", "Female",
                              "Rarely", "Regularly", "Unemployed", "Student", "Not in workforce",
                              "Household size", "Interview distance to previous election", "Not born in country of residence", "Minority", "Cannot vote", "Log unemployment rate", "Log GDP", "% foreign born", "Gini coefficient (Disponible income)", "Population density", "Parliament*Running variable", "Parliament*Running variable*Running variable"), 
        groups = list("Education (reference: No or primary education)" = 5:8, "Religious_attendance attendance (Reference: Never)" = 11:12, "Employment status (Reference: Employed)" =13:15, "Contextual factors" = 21:25), 
        caption = "Regression discontinuity design Regression table - Second-order polynomials",
        caption.above = TRUE, file = "Table_G1.doc", star.symbol = "\\*")

## Third-order polynomials

mod_RDD1c <- lm_robust(data = RDD_analysis,
                   Antiimmi ~ Parliament*Running + Parliament*Running2 + Parliament*Running3 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD1c)

mod_RDD1d <- lm_robust(data = RDD_analysis,
                   Pol_antiimmi ~ Parliament*Running + Parliament*Running2 + Parliament*Running3 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD1d)

mod_RDD2c <- lm_robust(data = RDD_analysis,
                   Redistribution ~ Parliament*Running + Parliament*Running2 + Parliament*Running3 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD2c)

mod_RDD2d <- lm_robust(data = RDD,
                   Wc ~ Parliament*Running + Parliament*Running2 + Parliament*Running3 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD2d)

mod_RDD3b <- lm_robust(data = RDD_analysis,
                   Institutional_trust ~ Parliament*Running + Parliament*Running2 + Parliament*Running3 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD3b)

mod_RDD4b <- lm_robust(data = RDD_analysis,
                   Social_trust ~ Parliament*Running + Parliament*Running2 + Parliament*Running3 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD4b)

htmlreg(list(mod_RDD1c, mod_RDD1d, mod_RDD2c, mod_RDD2d,mod_RDD3b, mod_RDD4b),
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.header = list("Anti-immigration" = 1, "Polarization of immigrant attitudes" =2, "Support for redistribution"=3, "Welfare chauvinism"=4, "Institutional trust"=5, "Social trust"=6),
        custom.coef.names = c("RRP in parliament", "RRP vote share in % relative to threshold", "Squared RRP vote share in % relative to threshold", "Cubic RRP vote share in % relative to threshold", "Vote share", "Lower secondary", "Upper secondary", "Post-secondary", "Tertiary", "Age in years (Z-score)", "Female", "Rarely", "Regularly", "Unemployed", "Student", "Not in workforce", "Household size", "Interview distance to previous election", "Not born in country of residence", "Minority", "Cannot vote", "Log unemployment rate", "Log GDP", "% foreign born", "Gini coefficient (Disponible income)", "Population density", "Parliament*Running variable", "Parliament*Running variable*Running variable", "Parliament*Running variable*Running variable*Running variable"), 
        groups = list("Education (reference: No or primary education)" = 3:7, "Religious_attendance attendance (Reference: Never)" = 10:11, "Employment status (Reference: Employed)" =12:14), 
        caption = "Regression discontinuity design regression table - Third-order polynomials",
        caption.above = TRUE, file = "Table_G2.doc", star.symbol = "\\*")

plotdata_RDD <- bind_rows(
  tidy(mod_RDD1a),
  tidy(mod_RDD1b),
  tidy(mod_RDD1c),
  tidy(mod_RDD1d),
  tidy(mod_RDD2a),
  tidy(mod_RDD2b),
  tidy(mod_RDD2c),
  tidy(mod_RDD2d),
  tidy(mod_RDD3),
  tidy(mod_RDD3b),
  tidy(mod_RDD4),
  tidy(mod_RDD4b)) %>% filter(str_detect(term,"^ParliamentRRP in parliament$"))

plotdata_RDD <- cbind(ID = 1:nrow(plotdata_RDD), plotdata_RDD)

plotdata_RDD <- plotdata_RDD %>%
mutate(
    min90 = (estimate - qt(0.95, df) * std.error),
    min95 = (estimate - qt(0.975, df) * std.error),
    max90 = (estimate + qt(0.95, df) * std.error),
    max95 = (estimate + qt(0.975, df) * std.error),
    label_up = round(estimate, digits = 3),
    term = "",
Hypo = case_when(
  outcome == "Antiimmi" ~ "H1a: Anti-immigration",
  outcome == "Pol_antiimmi" ~ "H1b: Polarization immigration",
  outcome == "Redistribution" ~ "H2a: Support for redistribution",
  outcome == "Wc" ~ "H2b: Welfare chauvinism",
  outcome == "Institutional_trust" ~ "H3: Institutional trust",
  outcome == "Social_trust" ~ "H4: Social trust"),
Polynomial = case_when(
  ID == 1 | ID == 2 | ID == 5 | ID == 6 | ID == 9 | ID == 11 ~ "2 polynomials",
  ID == 3 | ID == 4 | ID == 7 | ID == 8 | ID == 10 | ID == 12 ~ "3 polynomials") %>% factor() %>% fct_relevel("2 polynomials",
                                                                                                             "3 polynomials"))
## Plot   
Figure_G1 <- ggplot(data = plotdata_RDD, aes(y = estimate, x = term, shape = Polynomial)) + 
  geom_hline(yintercept = 0, color = "#901A1E", lty = "dashed") +
  geom_linerange(aes(ymin = min95, ymax = max95),
    color = "#808080", position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = min90, ymax = max90),
    size = 1.1, position = position_dodge(width = 0.5)) +
  geom_point(fill = "white", size = 2.5, position = position_dodge(width = 0.5)) +
  labs(x = "RRP in National Parliament", y = "Conditional Difference in attitude (in SD)") + 
  facet_wrap(Hypo ~ ., scales = "free")  + 
  coord_flip() +
  theme_bw() +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")

## Save Figure G1
ggsave("Figure_G1.pdf", plot = last_plot(), width = 8, height = 10)
#ggsave("Figure_G1.svg", plot = last_plot(), width = 8, height = 10)

plotdata_RDD_redis <- bind_rows(
  tidy(mod_RDD2a),
  tidy(mod_RDD2c)) %>% filter(str_detect(term,"^ParliamentRRP in parliament$"))

plotdata_RDD_redis <- cbind(ID = 1:nrow(plotdata_RDD_redis), plotdata_RDD_redis)



plotdata_RDD_redis <- plotdata_RDD_redis %>%
mutate(
    min90 = (estimate - qt(0.95, df) * std.error),
    min95 = (estimate - qt(0.975, df) * std.error),
    max90 = (estimate + qt(0.95, df) * std.error),
    max95 = (estimate + qt(0.975, df) * std.error),
    label_up = round(estimate, digits = 3),
    term = "",
Polynomial = case_when(
  ID == 1 ~ "2 polynomials",
  ID == 2 ~ "3 polynomials") %>% factor() %>% fct_relevel("2 polynomials", "3 polynomials"))
## Plot   

Figure_G1_redis <- ggplot(data = plotdata_RDD_redis, aes(y = estimate, x = term, shape = Polynomial)) + 
  geom_hline(yintercept = 0, color = "#901A1E", lty = "dashed") +
  geom_linerange(aes(ymin = min95, ymax = max95),
    color = "#808080", position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = min90, ymax = max90),
    size = 1.1, position = position_dodge(width = 0.5)) +
  geom_point(fill = "white", size = 2.5, position = position_dodge(width = 0.5)) +
  labs(x = "Radical right party in National Parliament", y = "Conditional Difference in support for redistribution (in SD)") + 
  coord_flip() +
  theme_bw() +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")

## Save Figure G1
ggsave("Figure_G1_redis.pdf", plot = last_plot(), width = 8, height = 10)
#ggsave("Figure_G1.svg", plot = last_plot(), width = 8, height = 10)


### Varying bandwidths: Support for redistribution ### 

BW3 <- lm_robust(data = RDD_analysis %>% filter(Running<=3 & Running>=-3),
                   Redistribution ~ Parliament*Running + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(BW3)


BW4 <- lm_robust(data = RDD_analysis %>% filter(Running<=4 & Running>=-4),
                   Redistribution ~ Parliament*Running + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(BW4)

BW5 <- lm_robust(data = RDD_analysis %>% filter(Running<=5),
                   Redistribution ~ Parliament*Running + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(BW5)

BW75 <- lm_robust(data = RDD_analysis %>% filter(Running<=7.5),
                   Redistribution ~ Parliament*Running + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(BW75)

BW10 <- lm_robust(data = RDD_analysis %>% filter(Running<=10),
                   Redistribution ~ Parliament*Running + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(BW10)


BWfull <- lm_robust(data = RDD_analysis,
                   Redistribution ~ Parliament*Running + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Distance_election + Born_country + Majority + Eligible_vote +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(BWfull)

plotdata_BW <-  bind_rows(
  tidy(BW3),
  tidy(BW4),
  tidy(BW5),
  tidy(BW75),
  tidy(BW10),
  tidy(BWfull)) %>% filter(str_detect(term,"^Parliament") & str_detect(term, "ParliamentRRP in parliament:", negate = TRUE))

plotdata_BW <- plotdata_BW %>%
mutate(
    min90 = (estimate - qt(0.95, df) * std.error),
    min95 = (estimate - qt(0.975, df) * std.error),
    max90 = (estimate + qt(0.95, df) * std.error),
    max95 = (estimate + qt(0.975, df) * std.error),
    label_up = round(estimate, digits = 3),
    term = "",
BW = case_when(
  df == 62 ~ "3",
  df == 74 ~ "4",
    df == 98 ~ "5",
    df == 112 ~ "7.5",
    df == 122 ~ "10",
    df == 175 ~ "Full sample") %>% as_factor())

## Plot   
Figure_G2 <- ggplot(data = plotdata_BW, aes(y = estimate, x = BW)) + 
  geom_hline(yintercept = 0, color = "#901A1E", lty = "dashed") +
  geom_linerange(aes(ymin = min95, ymax = max95),
    color = "#808080", position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = min90, ymax = max90),
    size = 1.1, position = position_dodge(width = 0.5)) +
  geom_point(fill = "white", size = 2.5, position = position_dodge(width = 0.5)) +
  labs(x = "Radical right party in national parliament (diff. bandwidths)", y = "Conditional difference in support for redistribution (in SD)") + 
  theme_bw() +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")

## Save Figure G2
ggsave("Figure_G2.pdf", plot = last_plot(), width = 8, height = 10)
#ggsave("Figure_G2.svg", plot = last_plot(), width = 8, height = 10)

Figure_2 <- ggarrange(Figure_G1_redis, Figure_G2, nrow = 2, labels  = "AUTO")

## Save Figure 2
ggsave("Figure_2.jpg", plot = Figure_2, width = 6, height = 10)
#ggsave("Figure_2.svg", plot = Figure_2, width = 8, height = 10)
```

\newpage
# Appendix H: RDD assumptions & robustness
```{r RDD robustness, results='hide'}

### Continuity assumption ###

Unemployment <- lm_robust(data = RDD_analysis,
                   Lnunemployment ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Unemployment)

Gdp <- lm_robust(data = RDD_analysis,
                   Lngdp ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lnunemployment + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Gdp)

Foreign <- lm_robust(data = RDD_analysis,
                   Foreign ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lngdp + Lnunemployment + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Foreign)

Gini <- lm_robust(data = RDD_analysis,
                   Gini ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lngdp + Foreign + Lnunemployment + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Gini)

Density <- lm_robust(data = RDD_analysis,
                   Density ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lngdp + Foreign + Gini + Lnunemployment,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Density)

Female <- lm_robust(data = RDD_analysis,
                   as.numeric(Female) ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Female)

Age <- lm_robust(data = RDD_analysis,
                   Age ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Age)

Householdsize <- lm_robust(data = RDD_analysis,
                   Household_size ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Born_country + Majority + Eligible_vote + Distance_election + Employment + 
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Householdsize)

Distance <- lm_robust(data = RDD_analysis,
                   Distance_election ~ Parliament*Running + Parliament*Running2 +Voteshare +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote +  
                   + Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Distance)

Education <- lm_robust(data = RDD_analysis,
                   as.numeric(Education) ~ Parliament*Running + Parliament*Running2 + Voteshare +
                   Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election + 
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Education)

plotdata_continuity <-  bind_rows(
  tidy(Unemployment),
  tidy(Gdp),
  tidy(Foreign),
  tidy(Gini),
  tidy(Density),
  tidy(Age),
  tidy(Female),
  tidy(Householdsize),
  tidy(Distance),
  tidy(Education)) %>% filter(str_detect(term,"^Parliament") & str_detect(term, "ParliamentRRP in parliament:", negate = TRUE))

plotdata_continuity <- plotdata_continuity %>%
  mutate(
    Name = case_when(
      outcome == "Lnunemployment" ~ "Logged unemployment rate",
      outcome == "Lngdp" ~ "Logged GDP",
      outcome == "Foreign" ~ "% foreign born",
      outcome == "Gini" ~ "Gini coefficient",
      outcome == "Density" ~ "Population density",
      outcome == "Age" ~ "Respondent's age",
      outcome == "as.numeric(Female)" ~ "Female respondents",
      outcome == "Household_size" ~ "Respondents household size",
      outcome == "as.numeric(Education)" ~ "Respondents educational attainment",
      outcome == "Distance_election" ~ "Survey response distance to previous election"))

## Plot   
Figure_H1 <- ggplot(data = plotdata_continuity, aes(y = p.value, x = Name)) + 
  geom_hline(yintercept = 0.05, color = "#901A1E", lty = "dashed") +
  geom_point(fill = "white", size = 2.5, position = position_dodge(width = 0.5)) +
  labs(x = "Populist Radical Right Party in Parliament", y = "P value") + 
  coord_flip() +
  theme_bw() +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")

## Save Figure H1
ggsave("Figure_H1.pdf", plot = last_plot(), width = 8, height = 10)
#ggsave("Figure_H1.svg", plot = last_plot(), width = 8, height = 10)

### Within group jumps ###

RDD_placebobelow <- RDD %>% select(Redistribution, Parliament_below, Running_below, Running_below2, Voteshare, Seatshare, Education, Employment, Age, Female, Religious_attendance, Household_size, Distance_election, Lnunemployment, Lngdp, Foreign, Gini, Density, Country, Time, Countryyear, Weight, Born_country, Majority, Eligible_vote) %>% drop_na()

RDD_placeboabove <- RDD %>% select(Redistribution, Parliament_above, Running_above, Running_above2, Voteshare, Seatshare, Education, Employment, Age, Female, Religious_attendance, Household_size, Distance_election, Lnunemployment, Lngdp, Foreign, Gini, Density, Country, Time, Countryyear, Weight, Born_country, Majority, Eligible_vote) %>% drop_na()

Placebobelow <- lm_robust(data = RDD_placebobelow,
                   Redistribution ~ Parliament_below + 
                   Parliament_below*Running_below + Parliament_below*Running_below2 + Voteshare + 
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Placebobelow)

Placeboabove <- lm_robust(data = RDD_placeboabove,
                   Redistribution ~ Parliament_above + 
                   Parliament_above*Running_above + Parliament_above*Running_above2 + Voteshare +
                   Education + Age + Female + Religious_attendance + 
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(Placeboabove)

htmlreg(list(Placeboabove, Placebobelow),
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.header = list("Placebo: Treatment group" = 1, "Placebo: Control group" = 2),
        custom.coef.names = c("RRP in parliament - treatment", "RRP vote share in % relative to threshold - treatment", "Squared RRP vote share in % relative to threshold - treatment", "RRP vote share",  "Lower secondary", "Upper secondary", "Post-secondary", "Tertiary", "Age in years", "Female",
                              "Rarely", "Regularly", "Unemployed", "Student", "Not in workforce",
                              "Household size", "Not born in country", "Minority", "Cannot vote", "Survey distance", "Log unemployment rate", "Log GDP", "% foreign born", "Gini coefficient (Disponible income)", "Population density",  "RRP in ParliamentXRunning - treatment", "RRP in ParliamentXRunning^2 - treatment", "RRP in parliament - control", "RRP vote share in % relative to threshold - control", "Squared RRP vote share in % relative to threshold - control", "RRP in ParliamentXRunning - control", "RRP in ParliamentXRunning^2  control"), 
        groups = list("Education (reference: No or primary education)" = 4:7, "Religious_attendance attendance (Reference: Never)" = 10:11, "Employment status (Reference: Employed)" =12:14, "Contextual factors"=20:24), 
        caption = "Placebo regressions - Within-group jumps",
        caption.above = TRUE, file = "Table_H1.doc", star.symbol = "\\*")


### Legal threshold ###

mod_RDD_legal <- lm_robust(data = RDD_analysis %>% filter(Legal_threshold==1),
                   Redistribution ~ Parliament*Running + Parliament*Running2 +
                   Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_RDD_legal)

mod_Fuzzy <- iv_robust(data = RDD_analysis,
                   Redistribution ~ Parliament + Education + Age + Female + Religious_attendance +
                   Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election + 
                   Lnunemployment + Lngdp + Foreign + Gini + Density | Running + Education + Age + Female + Religious_attendance + Employment + Household_size +
                   Born_country + Majority + Eligible_vote + Distance_election +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Countryyear,
                   se_type = "stata",
                   fixed_effects = ~ Country + Time,
                   weights = Weight)
summary(mod_Fuzzy)

htmlreg(list(mod_RDD_legal, mod_Fuzzy),
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.header = list("RDD: Only countries with legal threshold" = 1, "Fuzzy RDD: IV-regression" = 2),
        custom.coef.names = c("RRP in parliament", "RRP vote share in % relative to threshold", "Squared RRP vote share in % relative to threshold",
                              "Lower secondary", "Upper secondary", "Post-secondary", "Tertiary", "Age in years", "Female",
                              "Rarely", "Regularly", "Unemployed", "Student", "Not in workforce",
                              "Household size", "Not born in country", "Minority", "Cannot vote", "Survey distance", "Log unemployment rate", "Log GDP", "% foreign born", "Gini coefficient (Disponible income)", "Population density", "RRP in ParliamentXRunning", "RRP in ParliamentXRunning^2"), 
        groups = list("Education (reference: No or primary education)" = 4:7, "Religious_attendance attendance (Reference: Never)" = 10:11, "Employment status (Reference: Employed)" =12:14, "Contextual factors"=20:24), 
        caption = "Placebo regressions - Alt. model specifications",
        caption.above = TRUE, file = "Table_H2.doc", star.symbol = "\\*")
```

\newpage
# Appendix I: Exploratory analysis: The integration policy context as a moderator?
```{r Mipex}
FE_analysis <- read_dta("MIPEX data") %>% left_join(x = FE_analysis, y= ., by ="Countryyear")
mod_Mipex <- lm(data = FE_analysis,
                   Redistribution ~ mipex_overall*Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Country + Time,
                   weights = Weight)
summary(mod_Mipex)

Figure_I1a <- ggeffect(mod_Mipex, c("mipex_overall", "Parliament"),
                        vcov.fun = "vcovCL", 
                        vcov.type = "HC1",
                        vcov.args = list(cluster = FE_analysis$Country))
plot(Figure_I1a,
     show.title = FALSE,
     colors = "bw") + 
     labs(y = "Support for redistribution", x = "Mipex indicator of integration policy context") +
  theme(panel.grid.minor = element_line(colour="white"),
        legend.box = "vertical",
        legend.background = element_rect(fill=NA, color=NA),
        legend.key        = element_rect(fill =NA, color =NA),
        strip.background = element_rect(fill = "#f1f1f1", color = NA)) +
  theme(legend.position="bottom")

ggsave("Figure_3.jpg", plot = last_plot(), width = 10, height = 6)

mod_Mipex_labor <- lm(data = FE_analysis,
                   Redistribution ~ Parliament*mipex_labor + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Country + Time,
                   weights = Weight)
summary(mod_Mipex_labor)

Figure_I1b <- ggeffect(mod_Mipex_labor, c("mipex_labor", "Parliament"),
                        vcov.fun = "vcovCL", 
                        vcov.type = "HC1",
                        vcov.args = list(cluster = FE_analysis$Country))
plot(Figure_I1b,
     show.title = FALSE,
     colors = "bw")

ggsave("Figure_I1b.pdf", plot = last_plot(), width = 10, height = 6)

mod_Mipex_family <- lm(data = FE_analysis,
                   Redistribution ~ Parliament*mipex_familyreu + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Country + Time,
                   weights = Weight)
summary(mod_Mipex_family)

Figure_I1c <- ggeffect(mod_Mipex_family, c("mipex_familyreu", "Parliament"),
                        vcov.fun = "vcovCL", 
                        vcov.type = "HC1",
                        vcov.args = list(cluster = FE_analysis$Country))
plot(Figure_I1c,
     show.title = FALSE,
     colors = "bw")

ggsave("Figure_I1c.pdf", plot = last_plot(), width = 10, height = 6)


mod_Mipex_polit <- lm_robust(data = FE_analysis,
                   Redistribution ~ Parliament*mipex_polit + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Country + Time,
                   weights = Weight)
summary(mod_Mipex_polit)

Figure_I1d<- ggeffect(mod_Mipex_polit, c("mipex_polit", "Parliament"),
                        vcov.fun = "vcovCL", 
                        vcov.type = "HC1",
                        vcov.args = list(cluster = FE_analysis$Country))
plot(Figure_I1d,
     show.title = FALSE,
     colors = "bw")

ggsave("Figure_I1d.pdf", plot = last_plot(), width = 10, height = 6)


mod_Mipex_residence <- lm_robust(data = FE_analysis,
                   Redistribution ~ Parliament*mipex_residence  + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Country + Time,
                   weights = Weight)
summary(mod_Mipex_residence)

Figure_I1e <- ggeffect(mod_Mipex_residence, c("mipex_residence", "Parliament"),
                        vcov.fun = "vcovCL", 
                        vcov.type = "HC1",
                        vcov.args = list(cluster = FE_analysis$Country))
plot(Figure_I1e,
     show.title = FALSE,
     colors = "bw")

ggsave("Figure_I1e.pdf", plot = last_plot(), width = 10, height = 6)

mod_Mipex_citizenship <- lm_robust(data = FE_analysis,
                   Redistribution ~ Parliament*mipex_citizenship + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Country + Time,
                   weights = Weight)
summary(mod_Mipex_citizenship)

Figure_I1f <- ggeffect(mod_Mipex_citizenship, c("mipex_citizenship", "Parliament"),
                        vcov.fun = "vcovCL", 
                        vcov.type = "HC1",
                        vcov.args = list(cluster = FE_analysis$Country))
plot(Figure_I1f,
     show.title = FALSE,
     colors = "bw")
ggsave("Figure_I1f.pdf", plot = last_plot(), width = 10, height = 6)


mod_Mipex_antidiscrimination <- lm_robust(data = FE_analysis,
                   Redistribution ~ Parliament*mipex_antidiscrimination + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Country + Time,
                   weights = Weight)
summary(mod_Mipex_antidiscrimination)

Figure_I1g <- ggeffect(mod_Mipex_antidiscrimination, c("mipex_antidiscrimination", "Parliament"),
                        vcov.fun = "vcovCL", 
                        vcov.type = "HC1",
                        vcov.args = list(cluster = FE_analysis$Country))
plot(Figure_I1g,
     show.title = FALSE,
     colors = "bw")

ggsave("Figure_I1g.pdf", plot = last_plot(), width = 10, height = 6)

Figure_I1 <- ggarrange(Figure_I1a, Figure_I1b, Figure_I1c, Figure_I1d, Figure_I1e, Figure_I1f, Figure_I1g, nrow = 3, ncol = 4, labels  = "AUTO")
ggsave("Figure_I1.pdf", plot = Figure_I1, width = 12, height = 12)

```

\newpage
#Appendix J: Replication using International Social Survey Programme data - support for redistribution
```{r ISSP}
ISSP <- read_dta("ISSP data")


ISSP <- ISSP %>%
  mutate(
    countryyear = case_when(
      country==	"France"	&	issp==	3	~	1,
      country==	"France"	&	issp==	4	~	2,
      country==	"France"	&	issp==	5	~	3,
      country==	"Germany"	&	issp==	2	~	4,
      country==	"Germany"	&	issp==	3	~	5,
      country==	"Germany"	&	issp==	4	~	6,
      country==	"Germany"	&	issp==	5	~	7,
      country==	"Hungary"	&	issp==	2	~	8,
      country==	"Hungary"	&	issp==	3	~	9,
      country==	"Hungary"	&	issp==	4	~	10,
      country==	"Hungary"	&	issp==	5	~	11,
      country==	"Ireland"	&	issp==	2	~	12,
      country==	"Ireland"	&	issp==	3	~	13,
      country==	"Ireland"	&	issp==	4	~	14,
      country==	"Italy"	&	issp==	2	~	15,
      country==	"Italy"	&	issp==	3	~	16,
      country==	"Latvia"	&	issp==	3	~	17,
      country==	"Latvia"	&	issp==	4	~	18,
      country==	"Latvia"	&	issp==	5	~	19,
      country==	"Poland"	&	issp==	3	~	20,
      country==	"Poland"	&	issp==	4	~	21,
      country==	"Spain"	&	issp==	3	~	22,
      country==	"Spain"	&	issp==	4	~	23,
      country==	"Spain"	&	issp==	5	~	24,
      country==	"Sweden"	&	issp==	3	~	25,
      country==	"Sweden"	&	issp==	4	~	26,
      country==	"Sweden"	&	issp==	5	~	27,
      country==	"UK"	&	issp==	2	~	28,
      country==	"UK"	&	issp==	3	~	29,
      country==	"UK"	&	issp==	4	~	30,
      country==	"UK"	&	issp==	5	~	31))
ISSP <- read_dta("ISSP macrodata") %>% select(-c("issp")) %>% left_join(x = ISSP, y= ., by ="countryyear")

ISSP <- ISSP %>%
mutate(
  Parliament = case_when(
    parliament == 1 ~ "RRP in parliament",
    parliament == 0 ~ "RRP not in parliament") %>% factor() %>%
    fct_relevel("RRP not in parliament", "RRP in parliament"),
  redistribution = case_when(
    v27 == 1 | V56 == 1 ~ 4,
    v27 == 2 | V56 == 2 ~ 3,
    v27 == 3 | V56 == 3 ~ 2,
    v27 == 4 | V56 == 4 ~ 1),
  Redistribution = (redistribution - mean(redistribution, na.rm=TRUE)) / sd(redistribution, na.rm = TRUE),
  Education = case_when(
    degree == 0 | DEGREE == 0 ~ "No education",
    degree == 1 | DEGREE == 1 ~ "Primary",
    degree == 2 | degree == 3 | degree == 4 |DEGREE == 2 | DEGREE == 3 | DEGREE == 4 ~ "Secondary",
    degree == 5 | DEGREE == 5 | DEGREE == 6 ~ "Tertiary") %>% factor() %>% fct_relevel("No education","Primary","Secondary", "Tertiary"),
  Female = case_when(
    SEX == 1 | sex == 1 ~ "Male",
    SEX == 2 | sex == 2 ~ "Female") %>% factor() %>% fct_relevel("Male",
                                                            "Female"),
  age = case_when(
  age < 18 ~ NA_real_, TRUE ~ zap_labels(age)),
  Age = (age - mean(age, na.rm=TRUE)) / sd(age, na.rm = TRUE),
  Voteshare = votepct,
  Seatshare = seats,
  Weight = wteven,
  Lnunemployment = (log(unemployment) - mean(log(unemployment), na.rm=TRUE)) / sd(log(unemployment), na.rm = TRUE),
  Lngdp = (log(gdp) - mean(log(gdp), na.rm=TRUE)) / sd(log(gdp), na.rm = TRUE),
  Foreign = (foreign - mean(foreign, na.rm=TRUE)) / sd(foreign, na.rm = TRUE),
  Density = (density - mean(density, na.rm=TRUE)) / sd(density, na.rm = TRUE),
  Gini = (gini - mean(gini, na.rm=TRUE)) / sd(gini, na.rm = TRUE),
  Country = as_factor(country),
  Countryyear  = as_factor(countryyear),
  Time = as_factor(issp)) %>% select(Parliament, Redistribution, Education, Female, Age, Voteshare, Seatshare, Lnunemployment, Lngdp, Foreign, Density, Gini, Country, Countryyear, Time, Weight) %>% drop_na()


mod_issp <- lm_robust(data = ISSP,
                   Redistribution ~ Parliament + Voteshare +
                   Education + Age + Female +
                   Lnunemployment + Lngdp + Foreign + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   weights = Weight,
                   fixed_effects = ~ Country + Time)
summary(mod_issp, CI = TRUE)

htmlreg(mod_issp,
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.coef.names = c("RRP in parliament", "RRP vote share in %",
                              "Primary", "Secondary", "Tertiary", "Age in years", "Female", "Log unemployment rate", "Log GDP", "% foreign born", "Gini coefficient (Disponible income)", "Population density"), 
        groups = list("Education (reference: No education)" = 3:5, "Country-year level covariates" = 8:12), 
        caption = "ISSP - 2-way FE model: Support for redistribution",
        caption.above = TRUE, file = "Table_J2.doc", star.symbol = "\\*")

```

# Appendix K: Replication using Integrated Values Study data - institutional trust & social trust
```{r IVS}
## Load data
IVS <- read_dta("IVS data") %>% select(S003, X003, X028, X007, X025R, X001, X002_02, X013, S020, S002EVS, S002VS, S002EVS, S002, E069_07, E069_17, A165, pwght, S017, wteven)

IVS <- IVS %>% 
  mutate(
wave = case_when(
  S002EVS == 2 | S002 == 2 ~ 2,
  S002 == 3 ~ 3,
  S002EVS == 3 | S002 == 4 ~ 4,
  S002EVS == 4 | S002 == 5 ~ 5,
  S002 == 6 ~ 6,
  S002EVS == 5 | S002 == 7 ~ 7),
country = case_when(
  S003 == 100 ~ "Bulgaria",
  S003 == 196 ~ "Cyprus",
  S003 == 203 ~ "Czechia",
  S003 == 233 ~ "Estonia",
  S003 == 246 ~ "Finland",
  S003 == 276 ~ "Greece",
  S003 == 348 ~ "Hungary",
  S003 == 352 ~ "Iceland",
  S003 == 372 ~ "Ireland",
  S003 == 380 ~ "Italy",
  S003 == 428 ~ "Latvia",
  S003 == 440 ~ "Lithuania",
  S003 == 442 ~ "Luxembourg",
  S003 == 528 ~ "Netherlands",
  S003 == 616 ~ "Poland",
  S003 == 620 ~ "Portugal",
  S003 == 724 ~ "Spain",
  S003 == 752 ~ "Sweden",
  S003 == 909 | S003 == 826 ~ "UK",
  S003 == 804 ~ "Ukraine") %>% as_factor(),
survey = case_when(
  S002EVS == 1 | S002EVS == 2 |  S002EVS == 3 | S002EVS == 4 | S002EVS == 5 ~ "evs",
  S002 == 1 | S002 == 2 | S002 == 3 | S002 == 4 | S002 == 5 | S002 == 6 | S002 == 7 ~ "wvs") %>% as_factor(),
countryyear = case_when(
country==	"Bulgaria"	&	survey==	"evs"	&	wave==	2	~	1	,
country==	"Bulgaria"	&	survey==	"wvs"	&	wave==	3	~	2	,
country==	"Bulgaria"	&	survey==	"evs"	&	wave==	4	~	3	,
country==	"Bulgaria"	&	survey==	"wvs"	&	wave==	5	~	4	,
country==	"Bulgaria"	&	survey==	"evs"	&	wave==	5	~	5	,
country==	"Bulgaria"	&	survey==	"evs"	&	wave==	7	~	6	,
country==	"Cyprus"	&	survey==	"wvs"	&	wave==	5	~	7	,
country==	"Cyprus"	&	survey==	"evs"	&	wave==	5	~	8	,
country==	"Cyprus"	&	survey==	"wvs"	&	wave==	6	~	9	,
country==	"Cyprus"	&	survey==	"wvs"	&	wave==	7	~	10	,
country==	"Czechia"	&	survey==	"evs"	&	wave==	2	~	11	,
country==	"Czechia"	&	survey==	"wvs"	&	wave==	2	~	12	,
country==	"Czechia"	&	survey==	"wvs"	&	wave==	3	~	13	,
country==	"Czechia"	&	survey==	"evs"	&	wave==	4	~	14	,
country==	"Czechia"	&	survey==	"evs"	&	wave==	5	~	15	,
country==	"Czechia"	&	survey==	"evs"	&	wave==	7	~	16	,
country==	"Estonia"	&	survey==	"evs"	&	wave==	2	~	17	,
country==	"Estonia"	&	survey==	"wvs"	&	wave==	3	~	18	,
country==	"Estonia"	&	survey==	"evs"	&	wave==	4	~	19	,
country==	"Estonia"	&	survey==	"evs"	&	wave==	5	~	20	,
country==	"Estonia"	&	survey==	"wvs"	&	wave==	6	~	21	,
country==	"Estonia"	&	survey==	"evs"	&	wave==	7	~	22	,
country==	"Finland"	&	survey==	"evs"	&	wave==	2	~	23	,
country==	"Finland"	&	survey==	"wvs"	&	wave==	3	~	24	,
country==	"Finland"	&	survey==	"evs"	&	wave==	4	~	25	,
country==	"Finland"	&	survey==	"wvs"	&	wave==	5	~	26	,
country==	"Finland"	&	survey==	"evs"	&	wave==	5	~	27	,
country==	"Finland"	&	survey==	"evs"	&	wave==	7	~	28	,
country==	"Germany"	&	survey==	"evs"	&	wave==	2	~	29	,
country==	"Germany"	&	survey==	"wvs"	&	wave==	3	~	30	,
country==	"Germany"	&	survey==	"evs"	&	wave==	4	~	31	,
country==	"Germany"	&	survey==	"wvs"	&	wave==	5	~	32	,
country==	"Germany"	&	survey==	"evs"	&	wave==	5	~	33	,
country==	"Germany"	&	survey==	"wvs"	&	wave==	6	~	34	,
country==	"Germany"	&	survey==	"evs"	&	wave==	7	~	35	,
country==	"Germany"	&	survey==	"wvs"	&	wave==	7	~	36	,
country==	"Greece"	&	survey==	"evs"	&	wave==	4	~	37	,
country==	"Greece"	&	survey==	"evs"	&	wave==	5	~	38	,
country==	"Greece"	&	survey==	"wvs"	&	wave==	7	~	39	,
country==	"Hungary"	&	survey==	"evs"	&	wave==	2	~	40	,
country==	"Hungary"	&	survey==	"wvs"	&	wave==	3	~	41	,
country==	"Hungary"	&	survey==	"evs"	&	wave==	4	~	42	,
country==	"Hungary"	&	survey==	"evs"	&	wave==	5	~	43	,
country==	"Hungary"	&	survey==	"wvs"	&	wave==	5	~	44	,
country==	"Hungary"	&	survey==	"evs"	&	wave==	7	~	45	,
country==	"Iceland"	&	survey==	"evs"	&	wave==	4	~	46	,
country==	"Iceland"	&	survey==	"evs"	&	wave==	5	~	47	,
country==	"Iceland"	&	survey==	"evs"	&	wave==	7	~	48	,
country==	"Ireland"	&	survey==	"evs"	&	wave==	2	~	49	,
country==	"Ireland"	&	survey==	"evs"	&	wave==	4	~	50	,
country==	"Ireland"	&	survey==	"evs"	&	wave==	5	~	51	,
country==	"Italy"	&	survey==	"evs"	&	wave==	2	~	52	,
country==	"Italy"	&	survey==	"evs"	&	wave==	4	~	53	,
country==	"Italy"	&	survey==	"wvs"	&	wave==	5	~	54	,
country==	"Italy"	&	survey==	"evs"	&	wave==	5	~	55	,
country==	"Italy"	&	survey==	"evs"	&	wave==	7	~	56	,
country==	"Latvia"	&	survey==	"evs"	&	wave==	2	~	57	,
country==	"Latvia"	&	survey==	"wvs"	&	wave==	3	~	58	,
country==	"Latvia"	&	survey==	"evs"	&	wave==	4	~	59	,
country==	"Latvia"	&	survey==	"evs"	&	wave==	5	~	60	,
country==	"Lithuania"	&	survey==	"evs"	&	wave==	2	~	61	,
country==	"Lithuania"	&	survey==	"wvs"	&	wave==	3	~	62	,
country==	"Lithuania"	&	survey==	"evs"	&	wave==	4	~	63	,
country==	"Lithuania"	&	survey==	"evs"	&	wave==	5	~	64	,
country==	"Lithuania"	&	survey==	"evs"	&	wave==	7	~	65	,
country==	"Luxembourg"	&	survey==	"evs"	&	wave==	4	~	66	,
country==	"Luxembourg"	&	survey==	"evs"	&	wave==	5	~	67	,
country==	"Netherlands"	&	survey==	"evs"	&	wave==	2	~	68	,
country==	"Netherlands"	&	survey==	"evs"	&	wave==	4	~	69	,
country==	"Netherlands"	&	survey==	"wvs"	&	wave==	5	~	70	,
country==	"Netherlands"	&	survey==	"evs"	&	wave==	5	~	71	,
country==	"Netherlands"	&	survey==	"evs"	&	wave==	7	~	72	,
country==	"Poland"	&	survey==	"evs"	&	wave==	2	~	73	,
country==	"Poland"	&	survey==	"wvs"	&	wave==	3	~	74	,
country==	"Poland"	&	survey==	"evs"	&	wave==	4	~	75	,
country==	"Poland"	&	survey==	"wvs"	&	wave==	5	~	76	,
country==	"Poland"	&	survey==	"evs"	&	wave==	5	~	77	,
country==	"Poland"	&	survey==	"wvs"	&	wave==	6	~	78	,
country==	"Poland"	&	survey==	"evs"	&	wave==	7	~	79	,
country==	"Portugal"	&	survey==	"evs"	&	wave==	4	~	80	,
country==	"Portugal"	&	survey==	"evs"	&	wave==	5	~	81	,
country==	"Portugal"	&	survey==	"evs"	&	wave==	7	~	82	,
country==	"Spain"	&	survey==	"evs"	&	wave==	2	~	83	,
country==	"Spain"	&	survey==	"wvs"	&	wave==	2	~	84	,
country==	"Spain"	&	survey==	"wvs"	&	wave==	3	~	85	,
country==	"Spain"	&	survey==	"evs"	&	wave==	4	~	86	,
country==	"Spain"	&	survey==	"wvs"	&	wave==	4	~	87	,
country==	"Spain"	&	survey==	"wvs"	&	wave==	5	~	88	,
country==	"Spain"	&	survey==	"evs"	&	wave==	5	~	89	,
country==	"Spain"	&	survey==	"wvs"	&	wave==	6	~	90	,
country==	"Spain"	&	survey==	"evs"	&	wave==	7	~	91	,
country==	"Sweden"	&	survey==	"wvs"	&	wave==	2	~	92	,
country==	"Sweden"	&	survey==	"evs"	&	wave==	2	~	93	,
country==	"Sweden"	&	survey==	"wvs"	&	wave==	3	~	94	,
country==	"Sweden"	&	survey==	"evs"	&	wave==	4	~	95	,
country==	"Sweden"	&	survey==	"wvs"	&	wave==	5	~	96	,
country==	"Sweden"	&	survey==	"evs"	&	wave==	5	~	97	,
country==	"Sweden"	&	survey==	"wvs"	&	wave==	6	~	98	,
country==	"Sweden"	&	survey==	"evs"	&	wave==	7	~	99	,
country==	"UK"	&	survey==	"evs"	&	wave==	2	~	100	,
country==	"UK"	&	survey==	"wvs"	&	wave==	3	~	101	,
country==	"UK"	&	survey==	"evs"	&	wave==	4	~	102	,
country==	"UK"	&	survey==	"wvs"	&	wave==	5	~	103	,
country==	"UK"	&	survey==	"evs"	&	wave==	5	~	104	,
country==	"UK"	&	survey==	"evs"	&	wave==	7	~	105	,
country==	"Ukraine"	&	survey==	"wvs"	&	wave==	3	~	106	,
country==	"Ukraine"	&	survey==	"evs"	&	wave==	4	~	107	,
country==	"Ukraine"	&	survey==	"wvs"	&	wave==	5	~	108	,
country==	"Ukraine"	&	survey==	"evs"	&	wave==	5	~	109	,
country==	"Ukraine"	&	survey==	"wvs"	&	wave==	6	~	110	,
country==	"Ukraine"	&	survey==	"wvs"	&	wave==	7	~	111	,
country==	"Ukraine"	&	survey==	"evs"	&	wave==	7	~	112))	

IVS <- read_dta("IVS macrodata") %>% 
  select(-c("ivs", "survey", "country", "wave")) %>% left_join(x = IVS, y= ., by ="countryyear")

## Recode variables
IVS_analysis <- IVS %>%
    mutate(
      Trust_parliament = case_when(
        E069_07 == 1 ~ 4,
        E069_07 == 2 ~ 3,
        E069_07 == 3 ~ 2,
        E069_07 == 4 ~ 1),
      Trust_courts = case_when(
        E069_17 == 1 ~ 4,
        E069_17 == 2 ~ 3,
        E069_17 == 3 ~ 2,
        E069_17 == 4 ~ 1),
      Social_trust = case_when(
        A165 == 2 ~ 0,
        A165 == 1 ~ 1),
      Parliament = case_when(
        parliament == 1 ~ "RRP in parliament",
        parliament == 0 ~ "RRP not in parliament") %>% factor() %>% fct_relevel("RRP not in parliament", "RRP in parliament"),
      age = case_when(
        X003 < 18 ~ NA_real_, TRUE ~ zap_labels(X003)),
      Age = (age - mean(age, na.rm=TRUE)) / sd(age, na.rm = TRUE),
      Employment = case_when(
        X028 == 1 ~ "Full time",
        X028 == 2 ~ "Part time",
        X028 == 3 ~ "Self employed",
        X028 == 4 ~ "Retired",
        X028 == 5 ~ "Housewife",
        X028 == 6 ~ "Student",
        X028 == 7 ~ "Unemployed") %>% factor() %>% fct_relevel("Full time", "Part time", "Self employed", "Retired", "Housewife", "Unemployed"),
      Marital = case_when(
        X007 == 1 ~ "Married",
        X007 == 2 ~ "Cohabit",
        X007 == 3 ~ "Divorced",
        X007 == 4 ~ "Separated",
        X007 == 5 ~ "Widowed",
        X007 == 6 ~ "Single") %>% factor() %>% fct_relevel("Married", "Cohabit", "Divorced", "Separated", "Widowed", "Single"),
      Education = case_when(
        X025R == 1 ~ "Low",
        X025R == 2 ~ "Medium",
        X025R == 3 ~ "High") %>% factor() %>% fct_relevel("Low", "Medium", "High"),
      Female = case_when(
        X001 == 1 ~ "Male",
        X001 == 2 ~ "Female") %>% factor() %>% fct_relevel("Male", "Female"),
      Time = as_factor(S020),
      Country = as_factor(country),
      Countryyear = as_factor(countryyear),
      Survey = as_factor(survey),
      Wave = as_factor(wave),
      Weight = wteven,
      Lnunemployment = (log(unemployment) - mean(log(unemployment), na.rm=TRUE)) / sd(log(unemployment), na.rm = TRUE),
      Lngdp = (log(gdp) - mean(log(gdp), na.rm=TRUE)) / sd(log(gdp), na.rm = TRUE),
      Density = (density - mean(density, na.rm=TRUE)) / sd(density, na.rm = TRUE),
      Gini = (gini - mean(gini, na.rm=TRUE)) / sd(gini, na.rm = TRUE),
      Voteshare = voteshare,
      Seatshare = seatshare) %>% select(Trust_parliament, Trust_courts, Social_trust, Age, Employment, Marital, Education, Female, Time, Countryyear, Country, Time, Survey, Wave, Lnunemployment, Lngdp, Density, Gini, Voteshare, Seatshare, Parliament, S017) %>% drop_na()

## Calculate weights
#IVS_analysis$W <- sum(IVS_analysis$S017)
#w <- group_by(IVS_analysis, Countryyear) %>% summarise(w = sum(IVS_analysis$S017))
#IVS_analysis <- left_join(IVS_analysis, w, by=c("Countryyear"))
#IVS_analysis$Weight <- IVS_analysis$S017 * (IVS_analysis$W/IVS_analysis$w)
  
## PCA of Institutional trust
(pca_trust_institutions <- prcomp(formula = ~ Trust_parliament + Trust_courts, data = IVS_analysis, na.action = na.exclude, retx=TRUE))
  summary(pca_trust_institutions)

IVS_analysis$Trust_institutions <- pca_trust_institutions$x[ , "PC1"] # Generate new variable containing the PC1 scores.

mod_ivs_institutions <- lm_robust(data = IVS_analysis,
                   Trust_institutions ~ Parliament + Voteshare +
                   Education + Age + Female + Employment + Marital + Survey +
                   Lnunemployment + Lngdp + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   weights = Weight,
                   fixed_effects = ~ Country + Time)
summary(mod_ivs_institutions)

mod_ivs_social <- lm_robust(data = IVS_analysis,
                   Social_trust ~ Parliament + Voteshare +
                   Education + Age + Female + Employment + Marital + Survey +
                   Lnunemployment + Lngdp + Gini + Density,
                   clusters = Country,
                   se_type = "stata",
                   weights = Weight,
                   fixed_effects = ~ Country + Time)
summary(mod_ivs_social)

htmlreg(list(mod_ivs_institutions, mod_ivs_social),
        digits = 3, doctype = FALSE, stars = c(0.001, 0.01, 0.05, 0.1), symbol = "+",
        custom.header = list("Institutional trust" = 1, "Social trust" = 2),
        custom.coef.names = c("RRP in parliament", "RRP vote share in %",
                              "Medium", "High", "Age in years", "Female",
                              "Part time", "Self-employed", "Retired", "Housewife", "Unemployed", "Student", "Cohabit", "Divorced", "Separated", "Widowed", "Single", "World Values Survey",
                              "Log unemployment rate", "Log GDP", "Gini coefficient (Disponible income)", "Population density"), 
        groups = list("Education (reference: No or primary education)" = 3:4, "Employment status (Reference: Employed full time)" =7:12, "Marital status (Reference: Married)" = 13:17, "Country-year level controls" = 19:22), 
        caption = "IVS - 2-way FE model: Trust",
        caption.above = TRUE, file = "Table_K2.doc", star.symbol = "\\*")
```

# Appendix L: Assessing the findings using Bayes factors
```{r Bayes}
H1a_full <- lm(data = FE_analysis,
                   Antiimmi ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H1a_full)

H1a_null <- lm(data = FE_analysis,
                   Antiimmi ~ Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H1a_null)
BF_H1a = (1/exp((BIC(H1a_null) - BIC(H1a_full))/2))
BF_H1a # Strong evidence for the null hypothesis.

H1b_full <- lm(data = FE_analysis,
                   Pol_antiimmi ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H1b_full)

H1b_null <- lm(data = FE_analysis,
                   Pol_antiimmi ~ Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H1b_null)
BF_H1b = (1/exp((BIC(H1b_null) - BIC(H1b_full))/2))
BF_H1b # Strong evidence for the null hypothesis.


H2a_full <- lm(data = FE_analysis,
                   Redistribution ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H2a_full)

H2a_null <- lm(data = FE_analysis,
                   Redistribution ~ Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H2a_null)
BF_H2a = (1/exp((BIC(H2a_null) - BIC(H2a_full))/2))
BF_H2a # Strong evidence for the alternative hypothesis.

H3a_full <- lm(data = FE_analysis,
                   Institutional_trust ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H3a_full)

H3a_null <- lm(data = FE_analysis,
                   Institutional_trust ~ Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H3a_null)
BF_H3a = (1/exp((BIC(H3a_null) - BIC(H3a_full))/2))
BF_H3a # Strong evidence for the null hypothesis.

H4a_full <- lm(data = FE_analysis,
                   Social_trust ~ Parliament + Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H4a_full)

H4a_null <- lm(data = FE_analysis,
                   Social_trust ~ Voteshare
                   + Education + Age + Female + Religious_attendance 
                   + Employment + Household_size + Born_country + Majority + Eligible_vote + Distance_election +
                  Lnunemployment + Lngdp + Foreign + Gini + Density + Time + Country,
                   weights = Weight)
BIC(H4a_null)
BF_H4a = (1/exp((BIC(H4a_null) - BIC(H4a_full))/2))
BF_H4a # Strong evidence for the null hypothesis.
```


